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
- <? XML version= "1.0" encoding="UTF-8"?>
- <books>
- <book id="> "
- <name>thinking in Java</name>
- <price>85.5</price>
- </book>
- <book id="> "
- <name>spring in Action</name>
- <price>39.0</price>
- </book>
- </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
- Import Java.io.InputStream;
- Import java.util.ArrayList;
- Import java.util.List;
- Import Javax.xml.parsers.DocumentBuilder;
- Import Javax.xml.parsers.DocumentBuilderFactory;
- Import org.w3c.dom.Document;
- Import org.w3c.dom.Element;
- Import org.w3c.dom.NodeList;
- Import Org.w3c.dom.Node;
- Import Com.xtlh.cn.entity.Book;
- Public class Domparseservice {
- Public list<book> getbooks (InputStream inputstream) throws exception{
- list<book> list = new arraylist<book> ();
- Documentbuilderfactory factory = Documentbuilderfactory.newinstance ();
- Documentbuilder builder = Factory.newdocumentbuilder ();
- Document document = Builder.parse (InputStream);
- Element element = Document.getdocumentelement ();
- NodeList booknodes = Element.getelementsbytagname ("book");
- For (int i=0;i<booknodes.getlength (); i++) {
- Element bookelement = (Element) booknodes.item (i);
- Book book = new book ();
- Book.setid (Integer.parseint (Bookelement.getattribute ("id")));
- NodeList childNodes = Bookelement.getchildnodes ();
- SYSTEM.OUT.PRINTLN ("* * * * *" +childnodes.getlength ());
- For (int j=0;j<childnodes.getlength (); j + +) {
- if (Childnodes.item (j). Getnodetype () ==node.element_node) {
- if ("name". Equals (Childnodes.item (j). Getnodename ())) {
- Book.setname (Childnodes.item (j). Getfirstchild (). Getnodevalue ());
- }Else if ("price". Equals (Childnodes.item (j). Getnodename ())) {
- Book.setprice (Float.parsefloat (Childnodes.item (j). Getfirstchild (). Getnodevalue ());
- }
- }
- }//end for J
- List.add (book);
- }//end for i
- return list;
- }
- }
Book.java used to assemble data and put data
Java Code
- Public class Book {
- private int id;
- private String name;
- private float price;
- public int getId () {
- return ID;
- }
- public void setId (int id) {
- this.id = ID;
- }
- Public String GetName () {
- return name;
- }
- public void SetName (String name) {
- this.name = name;
- }
- Public Float GetPrice () {
- return price;
- }
- public void Setprice (float price) {
- this.price = Price;
- }
- @Override
- Public String toString () {
- return this.id+":" +this.name+":" +This.price;
- }
- }
The test uses unit tests as follows Parsetest.java
Java Code
- Public class Parsetest extends testcase{
- public void Testdom () throws exception{
- InputStream input = this.getclass (). getClassLoader (). getResourceAsStream ("Book.xml");
- Domparseservice dom = new Domparseservice ();
- list<book> books = dom.getbooks (input);
- For (book book:books) {
- System.out.println (Book.tostring ());
- }
- }
- }
Java DOM Parsing xml