The first native parsing method DOM
When parsing XML documents, the parser will write the contents of the documents into memory. If the document nodes are added, deleted or modified, they must be used
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(new DOMSource(doc), new StreamResult("src/dom4f.xml")); synchronize to local document
new DOMSource(doc) is the data loaded into memory
new StreamResult("src/dom4f.xml"); the result stream to be written
Create an xml document
<?xml version="1.0" encoding="UTF-8"?> < Library > < bookcase id = "003" > < book > How is steel made < member price > 15.8 < / member price > < author > trovsky < / author > < press > People's daily < / press > < member price > 10.0 < / member price > < book > < book > < title > dream of Red Mansions < / Title > < author > Cao Xueqin < / author > < press > Tinker < / press > < price > 18.8 < / price > < book > < bookshelf > < bookcase id = "002" > < book > How steel is made 11 < member price > 15.81 < / member price > < author > trovsky 1 < / author > < press > People's daily 1 < / press > < book > < book > < title > dream of Red Mansions 1 < / Title > < author > Cao Xueqin 1 < / author > < press > Tinker 1 < / press > < price > 18.81 < / price > < book > < bookshelf > < Library >
package dom; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.junit.Test; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Demo { @Test public void myTest_Dom() throws Exception { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document doc = documentBuilder.parse("src/dom4f.xml"); // Node node1= doc.getElementsByTagName("author"). item(0); // String content = node1.getTextContent(); // System.out.println(content); // Element newElement = doc.createElement("price"); // newElement.setTextContent("18.8"); // Node secondNode = doc.getElementsByTagName("book"). item(1); // Node node = secondNode.appendChild(newElement); // // Transformer tran = TransformerFactory.newInstance().newTransformer(); // tran.transform(new DOMSource(doc), new StreamResult("src/dom4f.xml")); //myTest2(doc); // myTest4(doc); // myTest5(doc); // myTest6(doc); myTest7(doc); System.out.println("========="); } private void myTest2(Node node) { if (node.getNodeType() == Node.ELEMENT_NODE) { System.out.println(node.getNodeName()); } NodeList childNodes = node.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { myTest2(childNodes.item(i)); } // TODO Auto-generated method stub } private void myTest4(Document doc) throws Exception { Element newElement = doc.createElement("Membership price"); newElement.setTextContent("15.8"); Node author = doc.getElementsByTagName("book").item(0); author.getParentNode().insertBefore(newElement, author); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.transform(new DOMSource(doc), new StreamResult("src/dom4f.xml")); } private void myTest5(Document doc) throws Exception { Node publisher = doc.getElementsByTagName("book").item(1); publisher.getParentNode().removeChild(publisher); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.transform(new DOMSource(doc), new StreamResult("src/dom4f.xml")); } /*private void myTest6(Document doc) throws Exception { Element element = (Element)doc.getElementsByTagName("Member price "). item(0) ֵ; element.setTextContent("19.8"); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.transform(new DOMSource(doc), new StreamResult("src/dom4f.xml")); }*/ private void myTest7(Document doc) throws Exception { Element element = (Element)doc.getElementsByTagName("ͼ���").item(0); element.setAttribute("address","Hefei, Anhui"); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.transform(new DOMSource(doc), new StreamResult("src/book.xml")); } }
The second way is that SAX parsing is performed according to the triggering of the event
package SAX_text; import java.io.File; import java.io.IOException; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class Text { public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException { SAXParserFactory saxParserFactory=SAXParserFactory.newInstance(); SAXParser saxParser = saxParserFactory.newSAXParser(); File file=new File("src/dom4f.xml"); saxParser.parse(file, new myHandler()); } } class myHandler extends DefaultHandler{ private boolean isExits=false; int i=0; @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if ("Title".equals(qName)) { // System.out.println(qName); isExits=true; i++; } // System.out.println(qName); } @Override public void endElement(String uri, String localName, String qName) throws SAXException { isExits=false; } @Override public void characters(char[] ch, int start, int length) throws SAXException { if (isExits&&i==2) { System.out.println(new String(ch,start,length)); } } }
The third way is DOM4J based on SAX
package dom4j_text; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.List; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.Node; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; public class Text { public static void main(String[] args) throws DocumentException, IOException { File file=new File("src/dom4f.xml"); SAXReader saxReader=new SAXReader(); Document doc = saxReader.read(file); Element root = doc.getRootElement(); /*// System.out.println(root+"111111111111"); Element element = root.element("Bookshelf ""; // System.out.println(element); List list1 = root.elements(); // System.out.println(list1); List list2 = element.elements(); // System.out.println(list2); Iterator iterator = list2.iterator(); while (iterator.hasNext()) { Element element2 = (Element)iterator.next(); System.out.println(element2.getName()); } */ //Change the id of bookshelf to id /* Attribute attribute = root.element("Bookshelf "). attribute("id "); attribute.setValue("003"); FileWriter writer=new FileWriter(file); doc.write(writer); writer.close();*/ //You can set the display style or the encoding format // OutputFormat sFormat=OutputFormat.createPrettyPrint(); // sFormat.setEncoding("UTF-8"); //The second book on the 003 shelf plus membership /*List<Element> list = root.selectNodes("Bookshelf [@ id=003] / book "); System.out.println(list.size()); Element element = list.get(0); Element addElement = element.addElement("Membership price "; addElement.addText("10.0"); XMLWriter writer=new XMLWriter(new FileWriter(file)); writer.write(doc); writer.close(); */ Node node = root.selectSingleNode("bookshelf[@id=003]/book[2]"); System.out.println(node.getText()); } }