When dealing with XML data in Androd phones, it is common to transfer data on different platforms, and we may use xml,xml as a platform-independent feature that is widely used in data communication, so how do you parse the XML file in Android?
There are usually three ways: dom,sax,pull
In this section we use DOM as a way to handle this.
DOM parsing XML is to first read the XML document into memory, and then use the DOM API to access the tree structure, and get the data, but this way, if the XML file is large? The CPU processing power of the mobile phone is certainly not comparable to the PC machine, so the processing efficiency is relatively poor, of course, this is for other ways to deal with XML documents.
Parsing an XML document, of course, must have an XML document file, I myself made a random river, put in the assets directory. The following:
<?xml version= "1.0" encoding= "Utf-8"?><rivers> <river name= "Lingqu" length= "605" > <introduction> Lingqu in the Xingan County of Guangxi Zhuang Autonomous Region, is one of the oldest canals in the world, and has the reputation of "the World Ancient Water Conservancy building pearl". Lingqu ancient known as Qin Chisel Ditch, 0 channels, Douhe, Xing ' an canal, in 214 BC Chisel into the navigation, 2,217 years away from now, still play a function. </introduction> <imageurl> Http://imgsrc.baidu.com/baike/pic/item/389aa8fdb7b8322e08244d3c.jpg & lt;/imageurl> </river> <river name= "Glue Rai Canal" length= "Huang" > <introduction> Rubber Lai Canal south from the Yellow Sea Lingshan Haikou, North to the Bohai Sea, Sanshandao, through the current Jiaonan, Jiaozhou, Pingdu, Gaomi, Changyi and Laizhou, and so on, a total length of 200 kilometers, watershed area of 5400 square kilometers, north and south through the Shandong Peninsula, communication Huang Bo two seas. The Yao Jia Cun Canal is a north-south diversion from the eastern watershed of Pingdu. South flow from the mouth of Hemp Bay into the Jiaozhouwan, South gum lai he heze, 30 km long. Beiliu by the Sea warehouse mouth into the Laizhouwan, for the North gum lai he heze, longer than 100 kilometers. </introduction> <imageurl> Http://imgsrc.baidu.com/baike/pic/item/389aa8fdb7b8322e08244d3c.jpg & lt;/imageurl> </river> <river name= "Northern Jiangsu Irrigation Canals" Length= "168" > <introduction> located in the northern part of the Huaihe River , West Hongze Lake side of the Gaoliangjian, through the Hongze, Qingpu, Huai ' An, Funing, Sheyang, Binhai and other six counties (districts), Dongzhi Pole port into the sea of large artificial river. Full length 168km. </introduction> <imageurl> Http://imgsrc.baidu.com/baikE/pic/item/389aa8fdb7b8322e08244d3c.jpg </imageurl> </river> </rivers>
So how to deal with it?
The specific ideas are:
* First Use Documentbuilderfactory to create a documentbuilderfactory instance
* Then use documentbuilderfactory to create Documentbuilder
* Then load the XML document,
* Then get the root node (Element) of the document,
* Then get a list of all the child nodes in the root node (NodeList),
* Then use to get the nodes that need to be read in the list of child nodes.
Of course we observe the node, I need to use a river object to save the data, abstract the River class
public class River implements Serializable { private static final long serialversionuid = 1L; private String name; Public String GetName () { return name; } public void SetName (String name) { this.name = name; } public int GetLength () { return length; } public void setLength (int length) { this.length = length; } Public String getintroduction () { return introduction; } public void Setintroduction (String introduction) { this.introduction = introduction; } Public String Getimageurl () { return imageurl; } public void Setimageurl (String imageurl) { this.imageurl = ImageUrl; } private int length; Private String introduction; Private String ImageUrl; }
Here we begin to read the XML Document object and add it into the list: the code is as follows:
Here we are using the River.xml file in assets, then we need to read the XML file and return the input stream.
Read by: Inputstream=this.context.getresources (). Getassets (). open (FileName); The parameter is the XML file path, and of course the default is the assets directory as the root directory.
You can then parse the input stream with the parse method of the Documentbuilder object, return the Document object, and then traverse the node properties of the Doument object.
Get all river Data/** * parameter fileName: FOR XML document PATH */public list<river> getriversfromxml (String fileName) {L Ist<river> rivers=new arraylist<river> (); Documentbuilderfactory Factory=null; Documentbuilder Builder=null; Document Document=null; InputStream Inputstream=null; First find the XML file Factory=documentbuilderfactory.newinstance (); try {//Find XML, and load document Builder=factory.newdocumentbuilder (); Inputstream=this.context.getresources (). Getassets (). open (FileName); Document=builder.parse (InputStream); Locate the root element element root=document.getdocumentelement (); NodeList Nodes=root.getelementsbytagname (river); Traverse all child nodes of the root node, rivers all river River=null; For (int. i=0;i<nodes.getlength (); i++) {River=new river (); Gets the river element node, elements riverelement= (Element) (Nodes.item (i)); Gets the Name property value in River River.setname (Riverelement.getattribute (name)); River.setlength (Integer.parseint (Riverelement.getattribute (LENGTH))); Get the river under Introduction label element introduction= (Element) Riverelement.getelementsbytagname (Introduction) . Item (0); River.setintroduction (Introduction.getfirstchild (). Getnodevalue ()); Element imageurl= (Element) Riverelement.getelementsbytagname (IMAGEURL). Item (0); River.setimageurl (Imageurl.getfirstchild (). Getnodevalue ()); Rivers.add (river); }}catch (IOException e) {e.printstacktrace (); } catch (Saxexception e) {e.printstacktrace (); } catch (Parserconfigurationexception e) {e.printstacktrace (); }finally{try {inputstream.close (); } catch (Ioexception e) {e.printstacktrace (); }} return rivers; }
Add them to the list here, and then we use the ListView to display them. :
Android parsing XML mode (i)