Preface
xml is a format for data transmission, and layout files, settings files, etc. in Android are represented by it. There are also many ways to parse xml files in Android. The following are 3 commonly used methods: Dom, SAX and dom4j. I won’t say much below, let’s take a look at the detailed introduction together.
Let's look at a simple xml file:
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <Book Publishing House="Horse"> <Book名>How to succeed</Book名> <author>uniapp</author> <Selling price>666</Selling price> </Book> <Book> <Book名>How to succeed1</Book名> <author>uniapp</author> <Selling price>1991</Selling price> </Book> </Book架>
1. Dom analysis method
Dom parses the entire xml file into memory at one time through the Document class, and then implements the addition, deletion, modification and search of elements in the xml file by operating the properties of the Document instance. The specific code is as follows:
2 SAX analysis
SAX uses the method of reading and parsing xml files while reading, just like we read articles with our eyes, doing it line by line. Compared with the instantaneous memory peak generated by the Dom method, SAX consumes memory relatively smoothly. It provides external interfaces through parsing classes, and implements them specifically:
public class SaxDemo { @Test public void main() throws ParserConfigurationException, SAXException, IOException{ //Factory class example SAXParserFactory fac = (); //Create a parser SAXParser parser = (); //Analyze the document XMLReader reader = (); (new MyDefultHandle()); ("./app/src/main/java/test/"); } } class MyDefultHandle extends DefaultHandler{ @Override public void startDocument() throws SAXException { (); ("Document Begins"); } @Override public void endDocument() throws SAXException { (); ("End of Document"); } private boolean isPrice = false; @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { (uri, localName, qName, attributes); ("Element Starts: " + qName); } @Override public void endElement(String uri, String localName, String qName) throws SAXException { (uri, localName, qName); if ("Sales Price".equals(qName)){ isPrice = false; } ("Element End: " + qName); } @Override public void characters(char[] ch, int start, int length) throws SAXException { (ch, start, length); if (isPrice){ ("content: " + new String(ch, start, length)); } } }
3 Dom4j analysis
Dom4j parsing is the parsing method given by the third open source library. It combines the advantages of Dom and SAX to gradually read xml files into memory and can access nodes in an object-oriented way. The code is as follows:
/** * Read attribute value * */ public void readAttr() throws DocumentException { Document doc = getDocument(); List<Element> list = ().elements("Book"); for (int i = 0; i < (); i++) { Element el = (Element) (i); Attribute att = ("Publishing House"); ("result: " + () + ()); } } /** * Read node * */ public void read() throws DocumentException { /** * Cannot get value beyond level * */ Document doc = getDocument(); org. el = (); Element firstEl = ("Book"); Element firstBookEl = ("Book Title"); String name = (); ("Title of the Book: " + name); } public void update() throws DocumentException, IOException { Document doc = getDocument(); List<Element> list = ().elements("Book"); Element element = (0); Element priceEl = ("Sales Price"); ("888 yuan"); XMLWriter writer = new XMLWriter(new FileOutputStream("./app/src/main/java/test/")); (doc); } /** * Delete nodes * */ public void deletePrice() throws Exception{ SAXReader reader = new SAXReader(); Document doc = ("./app/src/main/java/test/"); Element el = (Element) ().elements("Book").get(1); Element elPrice = (Element) ("Sales Price").get(1); ().remove(elPrice); XMLWriter writer = new XMLWriter(new FileOutputStream("./app/src/main/java/test/")); (doc); (); } /** * Add nodes * */ public void addEl() throws DocumentException, IOException { Document doc = getDocument(); Element el = (Element) ().elements("Book").get(1); ("Sales Price").setText("6.66 yuan"); XMLWriter writer = new XMLWriter(new FileOutputStream("./app/src/main/java/test/")); (doc); (); } private Document getDocument() throws DocumentException { SAXReader reader = new SAXReader(); return ("./app/src/main/java/test/"); }
Based on the characteristics of the above three analytical methods, we can conclude that if the xml file is very small, you can choose the object-oriented Dom or dom4j method; otherwise, you can choose the SAX method, which causes a crash when the actual memory is insufficient.
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.