Recursive DOM parsing (parse) XML

Source: Internet
Author: User

Friends.xml

[HTML]View plain copy
  1. <span style="FONT-SIZE:16PX;" ><? XML version= "1.0" encoding="Utf-8"?>
  2. <friends date="+">
  3. <!--here for comments--
  4. <friend id="1">
  5. < name > Little Red </name >
  6. <QQ>123456789</qq>
  7. </friend>
  8. <friend>
  9. < name > Xiaoping </name >
  10. <QQ>12345678</qq>
  11. </friend>
  12. <friend>
  13. < name mark="Hello"> Xiaomin </name >
  14. <QQ>1234567</qq>
  15. </friend>
  16. <friend>
  17. < name > Xiaoliang </name >
  18. <QQ>987654321</qq>
  19. </friend>
  20. <friend>
  21. < name > Xiaojian </name >
  22. <QQ>98765432</qq>
  23. </friend>
  24. </Friends>
  25. </span>

DOM Parse XML

[Java]View plain copy
  1. <span style="FONT-SIZE:16PX;"  >import java.io.File;
  2. Import Javax.xml.parsers.DocumentBuilderFactory;
  3. Import Javax.xml.parsers.DocumentBuilder;
  4. Import org.w3c.dom.Element;
  5. Import org.w3c.dom.Document;
  6. Import Org.w3c.dom.NamedNodeMap;
  7. Import org.w3c.dom.NodeList;
  8. Import org.w3c.dom.Attr;
  9. Import Org.w3c.dom.Node;
  10. Import org.w3c.dom.Comment;
  11. Use recursive DOM to parse any XML document, load the XML document into memory, occupy large memory, and randomly access
  12. Public class Recursiondom_xml
  13. {
  14. public static void Main (string[] args) throws Exception
  15. {
  16. //1. Getting the DOM parser factory
  17. Documentbuilderfactory dbf = Documentbuilderfactory.newinstance ();
  18. //2. Obtaining a specific DOM parser
  19. Documentbuilder db = Dbf.newdocumentbuilder ();
  20. //Gets the root node of the XML document
  21. Document document = Db.parse (new File ("Friends.xml"));
  22. //Gets the root element node of the document
  23. Element e = Document.getdocumentelement ();
  24. Parsexml (e);
  25. }
  26. public static void Parsexml (element Element)
  27. {
  28. //Get information about the element node
  29. String name = Element.getnodename ();
  30. System.out.print ("<" + name);
  31. //Gets the properties of this element node
  32. NamedNodeMap map = Element.getattributes ();
  33. //Gets the child node (including spaces) under the current element node (includes space) </span>
[Java]View plain copy
  1. <span style="FONT-SIZE:16PX;"  > NodeList child = Element.getchildnodes ();
  2. if (null! = map)
  3. {
  4. For (int i = 0; i < map.getlength (); i++)
  5. {
  6. Attr Attr = (Attr) map.item (i);
  7. String name1 = Attr.getname ();
  8. String value1 = Attr.getvalue ();
  9. System.out.print ("" + name1 +"=\" "+ value1 +" \ "");
  10. }
  11. }
  12. System.out.print (">");
  13. For (int j = 0; j < Child.getlength (); j + +)
  14. {
  15. Node node = Child.item (j);
  16. //Determine if the node is an element node, a recursive port
  17. if (node.getnodetype () = = Node.element_node)
  18. {
  19. //Start recursion
  20. Parsexml (Element) node);
  21. }
  22. //Judge if the node is not text, recursive exit
  23. Else if (node.getnodetype () = = Node.text_node)
  24. {
  25. System.out.print (Node.getnodevalue () + "");
  26. }
  27. //Determine if the node is a comment
  28. Else if (node.getnodetype () = = Node.comment_node)
  29. {
  30. System.out.print ("<!--");
  31. Comment Comment = (Comment) node;
  32. //comment content
  33. String data = Comment.getdata ();
  34. System.out.print (data);
  35. System.out.print ("--");
  36. }
  37. }
  38. System.out.println ("</" + name + ">");
  39. }
  40. }</span>

Recursive DOM parsing (parse) XML

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.