One: Read xml
- Dom parsing
- Sax parsing
- DOM4J parsing
- Jdom parsing
1.DOM parsing
<?XML version= "1.0" encoding= "UTF-8"?><Bookstore> < BookID= "1"> <name>The song of Ice and Fire</name> <author>George Martin</author> < Year>2014</ Year> < Price>89</ Price> </ Book> < BookID= "2"> <name>Andersen Fairy Tale</name> < Year>2004</ Year> < Price>77</ Price> <language>中文版</language> </ Book></Bookstore>
books.xml files such as
The parsing is as follows:
Packagecom.imooc.domtest.test;Importjava.io.IOException;ImportJavax.xml.parsers.DocumentBuilder;Importjavax.xml.parsers.DocumentBuilderFactory;Importjavax.xml.parsers.ParserConfigurationException;Importorg.w3c.dom.Document;Importorg.w3c.dom.Element;ImportOrg.w3c.dom.NamedNodeMap;ImportOrg.w3c.dom.Node;Importorg.w3c.dom.NodeList;Importorg.xml.sax.SAXException; Public classDomtest { Public Static voidMain (string[] args) {//to create an Documentbuilderfactory objectDocumentbuilderfactory DBF =documentbuilderfactory.newinstance (); //to create an Documentbuilder object Try { //Create a Documentbuilder objectDocumentbuilder db =Dbf.newdocumentbuilder (); //loading the books.xml file into the current project through the parser method of the Documentbuilder objectDocument document = Db.parse ("books.xml"); //gets the collection of all book nodesNodeList Booklist = document.getElementsByTagName ("book"); //The length of the Booklist can be obtained by NodeList's GetLength () methodSystem.out.println ("A total" + booklist.getlength () + "book"); //traverse each book node for(inti = 0; I < booklist.getlength (); i++) {System.out.println ("================= begins to traverse the" + (i + 1) + "The contents of this book ================="); //gets a book node with the item (i) method, with the index value of nodelist starting at 0Node book =Booklist.item (i);//gets all the property collections of the book NodeNamedNodeMap attrs =book.getattributes (); System.out.println ("No." + (i + 1) + "book Total" + attrs.getlength () + "Properties");//traversing the properties of a book for(intj = 0; J < Attrs.getlength (); J + +) { //get a property of a book node through the item (index) methodNode attr =Attrs.item (j); //Get Property nameSystem.out.print ("Property name:" +attr.getnodename ()); //Get Property valueSystem.out.println ("--attribute value" +Attr.getnodevalue ()); }// //Prerequisite: You already know that the book node has and can only have 1 ID attributes// //Convert a book node to an element type by forcing type conversion//element book = (element) booklist.item (i);// //Get property values by GetAttribute ("id") method//String attrValue = book.getattribute ("id");//System.out.println ("The attribute value of the id attribute is" + attrValue); //parsing the child nodes of the book nodeNodeList childNodes =book.getchildnodes (); //Traverse childnodes to get the node name and node value for each nodeSystem.out.println ("i+1" + "book in total" +childnodes.getlength ()+ "Child Nodes"); for(intk = 0; K < Childnodes.getlength (); k++) { //distinguishes between node of type text and element type. if(Childnodes.item (k). Getnodetype () = =Node.element_node) { //Gets the node name of the element type nodeSystem.out.print ("First" + (k + 1) + "node name of node:" +Childnodes.item (k). Getnodename ());//Gets the node value of the element type nodeSystem.out.println ("--node value is:" +Childnodes.item (k). Getfirstchild (). Getnodevalue ());//System.out.println ("--node value is:" + Childnodes.item (k). Gettextcontent ());}} System.out.println ("====================== End Traversal" + (i + 1) + "contents of this book ================="); } } Catch(parserconfigurationexception e) {e.printstacktrace (); } Catch(saxexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } }}
Two: Write XML
XML in Java