import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.w3c.dom.Node; import org.w3c.dom.Element; public class DOMParser { public static void main(String[] args) { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse("file.xml"); document.getDocumentElement().normalize(); NodeList nodeList = document.getElementsByTagName("book"); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) node; String title = element.getElementsByTagName("title").item(0).getTextContent(); String author = element.getElementsByTagName("author").item(0).getTextContent(); System.out.println("Title: " + title); System.out.println("Author: " + author); } } } catch (Exception e) { e.printStackTrace(); } } } import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; public class SAXParserExample { public static void main(String[] args) { try { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); DefaultHandler handler = new DefaultHandler() { boolean isTitle = false; boolean isAuthor = false; public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if (qName.equalsIgnoreCase("title")) { isTitle = true; } if (qName.equalsIgnoreCase("author")) { isAuthor = true; } } public void endElement(String uri, String localName, String qName) throws SAXException { if (qName.equalsIgnoreCase("book")) { System.out.println(); } } public void characters(char[] ch, int start, int length) throws SAXException { if (isTitle) { System.out.println("Title: " + new String(ch, start, length)); isTitle = false; } if (isAuthor) { System.out.println("Author: " + new String(ch, start, length)); isAuthor = false; } } }; saxParser.parse("file.xml", handler); } catch (Exception e) { e.printStackTrace(); } } } import java.io.File; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; public class JAXBExample { public static void main(String[] args) { try { File file = new File("file.xml"); JAXBContext jaxbContext = JAXBContext.newInstance(Book.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); Book book = (Book) jaxbUnmarshaller.unmarshal(file); System.out.println("Title: " + book.getTitle()); System.out.println("Author: " + book.getAuthor()); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); jaxbMarshaller.marshal(book, file); } catch (JAXBException e) { e.printStackTrace(); } } } import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlElement; @XmlRootElement public class Book { private String title; private String author; public String getTitle() { return title; } @XmlElement public void setTitle(String title) { this.title = title; } public String getAuthor() { return author; } @XmlElement public void setAuthor(String author) { this.author = author; } }


上一篇:
下一篇:
切换中文