Well, we still want to achieve the effect of this image, but we only need to change the technology to Dom. As for why the image here shows the sax parsing, I can only say, Hey, no way, I want to be lazy. Hey hey ...... Let's look at the code structure:
Main. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> <textview <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: id = "@ + ID/textview" <br/> </linearlayout> <br/>
Beauties. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <beauties> <br/> <beauty> <br/> <Name> Lin Zhiling </Name> <br/> <age> 28 </age> <br /> </beauty> <br/> <Name> Yang Mi </Name> <br/> <age> 23 </age> <br/> </beauty> </P> <p> </beauties> <br/>
Activity Code:
Package cn.com. dom; </P> <p> Import Java. io. inputstream; <br/> Import Java. util. arraylist; </P> <p> Import javax. XML. parsers. documentbuilder; <br/> Import javax. XML. parsers. documentbuilderfactory; </P> <p> Import Org. w3C. dom. document; <br/> Import Org. w3C. dom. element; <br/> Import Org. w3C. dom. node; <br/> Import Org. w3C. dom. nodelist; </P> <p> Import android. app. activity; <br/> Import android. OS. bundle; <br/> import Droid. util. log; <br/> Import android. widget. textview; </P> <p>/** <br/> * @ author chenzheng_java <br/> * @ description use Dom to parse XML <br/> *@ since 2011/03/04 <br/> */<br/> public class domparsexmltest extends activity {<br/> // stores the final content displayed on the foreground <br/> private string result = ""; </P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> Setcontentview (R. layout. main); </P> <p> inputstream = This. getclassloader (). getresourceasstream (<br/> "beauties. XML "); <br/> try {<br/> documentbuilderfactory factory = documentbuilderfactory <br/>. newinstance (); <br/> documentbuilder builder = factory. newdocumentbuilder (); <br/> document = builder. parse (inputstream); <br/> // obtain the root node <br/> element root = document. getdocumentelement (); </P> <p> parse (Root); </P> <p> for (beauty B: beautylist) {<br/> result + = B. tostring (); <br/>}</P> <p> textview = (textview) findviewbyid (R. id. textview); <br/> textview. settext (result); </P> <p >}catch (exception e) {<br/> E. printstacktrace (); <br/>}</P> <p> private Beauty = NULL; <br/> private arraylist <beauty> beautylist = new arraylist <beauty> (); </P> <p>/** <br/> * @ Param el Ement node to be traversed <br/> */<br/> private void parse (element) {<br/> nodelist = element. getchildnodes (); <br/> int size = nodelist. getlength (); <br/> for (INT I = 0; I <size; I ++) {<br/> // obtain the node at a specific position <br/> node element2 = (node) nodelist. item (I); <br/>/* getnodename get tagname, for example, getnodename of the <book> thinking in Android </book> element returns book <br/> * getnodetype returns the exact type of the current node, such as element, ATTR, and text <Br/> * getnodevalue returns the content of the node. If it is currently a text node, the content of the text is returned; otherwise, null is returned. <br/> * gettextcontent returns the text strings of the current node and its child nodes. These strings are concatenated into a string and returned to the user. For example, <br/> * calls this method for <book> <Name> thinking in Android </Name> <price> 12.23 </price> </book>, "Thinking in android12.23" <br/> */<br/> string tagname = element2.getnodename (); <br/> If (tagname. equals ("beauty") <br/> & amp; element2.getnodetype () = document. element_node) {<br/> beauty = new beauty (); <br/> log. I ("Notification:", "create beauty"); <br/> If (element2.getnodetype () = document. element_node) {<br/> parse (element) element2); <br/>}< br/> log. I ("Notification:", "add beauty"); <br/> beautylist. add (beauty); </P> <p >}</P> <p> If (tagname. equals ("name") {<br/> string name = element2.gettextcontent (); <br/> log. I ("Notification:", "name" + name); <br/> beauty. setname (name); </P> <p >}< br/> If (tagname. equals ("Age") {<br/> string age = element2.gettextcontent (); <br/> log. I ("Notification:", "Age" + age); <br/> beauty. setage (AGE ); </P> <p >}</P> <p>/** <br/> * <br/> * @ author chenzheng_java internal classes are used here for efficiency consideration, internal classes are more efficient and space-saving than single bean classes <br/> */<br/> private class beauty {<br/> string name; <br/> string age; </P> <p> Public String getname () {<br/> return name; <br/>}</P> <p> Public void setname (string name) {<br/> This. name = Name; <br/>}</P> <p> Public String getage () {<br/> return age; <br/>}</P> <p> Public void setage (string age) {<br/> This. age = age; <br/>}</P> <p> @ override <br/> Public String tostring () {<br/> return "Beauty information [age =" + age + ", name =" + name + "]"; <br/>}</P> <p >}< br/>}
Others are default. Run the command to view the correct result.
----------------------------------------------------------------------------------
When writing a blog, a child who does not summarize a few nonsense words is not a good child. So I decided to say a few words:
Although Dom Parsing is not recommended in Android, it does not mean it cannot be implemented. The principle of Dom is to regard all parts of the XML file as nodes, and all nodes form a node tree due to hierarchical relationships. The Dom parsing method is to survive the tree in the memory and allow users to perform related operations.
Here we list several common methods in Dom
Common Methods of node Interfaces
One node can call
Short getnodetype ()
Method returns a constant representing the node type (a constant value specified by the node interface). For example, for an element node, the value returned by the getnodetype () method is:
Node. element_node
Nodes can call
Nodelist getchildnodes ()
Returns a nodelist object consisting of all the subnodes of the current node. Node call
Node getfirstchild ()
Returns the first subnode of the current node. Node call
Node getlastchild ()
Returns the last subnode of the current node. Nodes can call
Nodelist gettextcontent ()
Return the text content of the current node and all child nodes.
There are many other methods, which can be detailed through APIS. This is mainly used to learn about Android, so you can get a little idea about Dom.