Java DOM Parsing xml

Source: Internet
Author: User

Dom parsing is to load the XML file, assemble it into a DOM tree, parse the XML file through the relationships between the nodes and the nodes, and parse the DOM with this XML file.

XML Code
  1. <? XML version= "1.0" encoding="UTF-8"?>
  2. <books>
  3. <book id="> "
  4. <name>thinking in Java</name>
  5. <price>85.5</price>
  6. </book>
  7. <book id="> "
  8. <name>spring in Action</name>
  9. <price>39.0</price>
  10. </book>
  11. </Books>

And then combine a graph to find out where Dom parsing should be noticed.



Here when we get the node book, which is where 1 of the picture is drawn, if we call its Getchildnodes () method, guess how many subnodes it has? It does not include its grandson node, except for thinking in Java, as it is the grandson node. It has a total of 5 child nodes, which are shown in Figures 2, 3, 4, 5, 6, respectively. So in the parsing, be careful, do not ignore the blank place.

Then look at the code to parse the Book.xml file

Domparseservice.java

Java Code
  1. Import Java.io.InputStream;
  2. Import java.util.ArrayList;
  3. Import java.util.List;
  4. Import Javax.xml.parsers.DocumentBuilder;
  5. Import Javax.xml.parsers.DocumentBuilderFactory;
  6. Import org.w3c.dom.Document;
  7. Import org.w3c.dom.Element;
  8. Import org.w3c.dom.NodeList;
  9. Import Org.w3c.dom.Node;
  10. Import Com.xtlh.cn.entity.Book;
  11. Public class Domparseservice {
  12. Public list<book> getbooks (InputStream inputstream) throws exception{
  13. list<book> list = new arraylist<book> ();
  14. Documentbuilderfactory factory = Documentbuilderfactory.newinstance ();
  15. Documentbuilder builder = Factory.newdocumentbuilder ();
  16. Document document = Builder.parse (InputStream);
  17. Element element = Document.getdocumentelement ();
  18. NodeList booknodes = Element.getelementsbytagname ("book");
  19. For (int i=0;i<booknodes.getlength (); i++) {
  20. Element bookelement = (Element) booknodes.item (i);
  21. Book book = new book ();
  22. Book.setid (Integer.parseint (Bookelement.getattribute ("id")));
  23. NodeList childNodes = Bookelement.getchildnodes ();
  24. SYSTEM.OUT.PRINTLN ("* * * * *" +childnodes.getlength ());
  25. For (int j=0;j<childnodes.getlength (); j + +) {
  26. if (Childnodes.item (j). Getnodetype () ==node.element_node) {
  27. if ("name". Equals (Childnodes.item (j). Getnodename ())) {
  28. Book.setname (Childnodes.item (j). Getfirstchild (). Getnodevalue ());
  29. }Else if ("price". Equals (Childnodes.item (j). Getnodename ())) {
  30. Book.setprice (Float.parsefloat (Childnodes.item (j). Getfirstchild (). Getnodevalue ());
  31. }
  32. }
  33. }//end for J
  34. List.add (book);
  35. }//end for i
  36. return list;
  37. }
  38. }

Book.java used to assemble data and put data

Java Code
  1. Public class Book {
  2. private int id;
  3. private String name;
  4. private float price;
  5. public int getId () {
  6. return ID;
  7. }
  8. public void setId (int id) {
  9. this.id = ID;
  10. }
  11. Public String GetName () {
  12. return name;
  13. }
  14. public void SetName (String name) {
  15. this.name = name;
  16. }
  17. Public Float GetPrice () {
  18. return price;
  19. }
  20. public void Setprice (float price) {
  21. this.price = Price;
  22. }
  23. @Override
  24. Public String toString () {
  25. return this.id+":" +this.name+":" +This.price;
  26. }
  27. }

The test uses unit tests as follows Parsetest.java

Java Code
    1. Public class Parsetest extends testcase{
    2. public void Testdom () throws exception{
    3. InputStream input = this.getclass (). getClassLoader (). getResourceAsStream ("Book.xml");
    4. Domparseservice dom = new Domparseservice ();
    5. list<book> books = dom.getbooks (input);
    6. For (book book:books) {
    7. System.out.println (Book.tostring ());
    8. }
    9. }
    10. }

Java DOM Parsing 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.