Java parses XML through DOM, and JavaDOM parses XML
1 package com. gcli. xml; 2 3 import java. io. IOException; 4 5 import javax. xml. parsers. documentBuilder; 6 import javax. xml. parsers. documentBuilderFactory; 7 import javax. xml. parsers. parserConfigurationException; 8 9 import org. w3c. dom. document; 10 import org. w3c. dom. element; 11 import org. w3c. dom. nodeList; 12 import org. xml. sax. SAXException; 13 14 public class XMLDOMTest {15 16 public static void main (St Ring [] args) {17 // (1) Create DocumentBuilderfactory to obtain DocumentBuilder18 DocumentBuilderFactory factory = DocumentBuilderFactory. newInstance (); 19 // (2) Obtain DocumentBuilder20 DocumentBuilder builder = null through DocumentBuilderfactory; 21 try {22 builder = factory. newDocumentBuilder (); 23} catch (ParserConfigurationException e) {24 e. printStackTrace (); 25} 26 // (3) define the Document Interface object and perform DOM tree conversion using the javasenbuilder class 27 Document doc = null; 28 try {29 doc = builder. parse ("C :\\ javatest \ bookstore. xml "); 30} catch (SAXException e) {31 e. printStackTrace (); 32} catch (IOException e) {33 e. printStackTrace (); 34} 35 // (4) Search for the book node 36 NodeList nl = doc. getElementsByTagName ("book"); 37 // (5) traverse the book node and output content 38 for (int I = 0; I <nl. getLength (); I ++) {39 Element element = (Element) nl. item (I); 40 String category = element. get Attribute ("category"); 41 String title = element. getElementsByTagName ("title "). item (0) 42. getFirstChild (). getNodeValue (); 43 String author = element. getElementsByTagName ("author "). item (0) 44. getFirstChild (). getNodeValue (); 45 String year = element. getElementsByTagName ("year "). item (0) 46. getFirstChild (). getNodeValue (); 47 String price = element. getElementsByTagName ("price "). item (0) 48. getFirstChild (). g EtNodeValue (); 49 System. out. println ("no." + (I + 1) + "book, category:" + category + ", name:" 50 + title + ", author:" + author + ", publication date: "+ year +", price: "51 + price + ". "); 52} 53} 54 55}
1 <?xml version="1.0" encoding="ISO-8859-1" ?> 2 - <bookstore> 3 - <book category="cooking"> 4 <title lang="en">Everyday Italian</title> 5 <author>Giada De Laurentiis</author> 6 <year>2005</year> 7 <price>30.00</price> 8 </book> 9 - <book category="children">10 <title lang="en">Harry Potter</title> 11 <author>J K. Rowling</author> 12 <year>2005</year> 13 <price>29.99</price> 14 </book>15 - <book category="web">16 <title lang="en">XQuery Kick Start</title> 17 <author>James McGovern</author> 18 <author>Per Bothner</author> 19 <author>Kurt Cagle</author> 20 <author>James Linn</author> 21 <author>Vaidyanathan Nagarajan</author> 22 <year>2003</year> 23 <price>49.99</price> 24 </book>25 - <book category="web" cover="paperback">26 <title lang="en">Learning XML</title> 27 <author>Erik T. Ray</author> 28 <year>2003</year> 29 <price>39.95</price> 30 </book>31 </bookstore>
How can I use a java program to parse xml files through DOM?
Create a Student object class to encapsulate data. I used to parse an example similar to your xml structure. You only need to modify it.
Public static List <Student> readXml (){
List <Student> list = new ArrayList <Student> ();
// Define a dom parser factory instance
DocumentBuilderFactory factory = DocumentBuilderFactory. newInstance ();
Try {
// Get a dom parser from the factory instance
DocumentBuilder dom = factory. newDocumentBuilder ();
// Find the xml document
File file = new File ("src/com/jereh/ch05/Students. xml ");
Document doc = dom. parse (file );
//
Element root = doc. getDocumentElement ();
NodeList stuNodeList = root. getChildNodes ();
For (int I = 0; I <stuNodeList. getLength (); I ++ ){
Node stu = stuNodeList. item (I );
Student student = new Student ();
If (stu! = Null & stu. getNodeType () = Node. ELEMENT_NODE ){
// System. out. println (stu );
Element stuElement = (Element) stu;
Student. setNo (stuElement. getAttribute ("id "));
// Stu. getAttributes (). getNamedItem (null );
// Element stu = (Element) stuNodeList
NodeList info = stuElement. getChildNodes ();
For (int j = 0; j <info. getLength (); j ++ ){
Info. item (j). getNodeName ();
Node n = info. item (j );
... The remaining full text>
Java dom parsing xml
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;
Public class FileTest {
/**
* @ Param args
*/
Public static void main (String [] args ){
DocumentBuilderFactory dbf = DocumentBuilderFactory. newInstance ();
Try {
DocumentBuilder db = dbf. newDocumentBuilder ();
Document doc = db. parse ("d:/test. xml ");
// Obtain the root node
Element root = doc. getDocumentElement ();
NodeList nl = root. getElementsByTagName ("HLR ");
Element e = (Element) nl. item (0 );
String hlrId = e. getAttribute ("HlrId ");
System. out. println (hlrId );
} Catch (Exception e ){
E. printStackTrace ();
}
}
}