It Programmer development Essentials-all kinds of resources download list, history of the most IT resources, personal collection summary. package EDU.JVM;
Import java.io.IOException;
Import Javax.xml.parsers.DocumentBuilder;
Import Javax.xml.parsers.DocumentBuilderFactory;
Import javax.xml.parsers.ParserConfigurationException;
Import Org.junit.Test;
Import org.w3c.dom.Document;
Import org.w3c.dom.Element;
Import Org.w3c.dom.Node;
Import org.w3c.dom.NodeList;
Import org.xml.sax.SAXException;
Import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List;
public class Jaxp_dom {
Get the Document object that represents the documents
@Test
public void Analydocument () throws Saxexception, IOException, parserconfigurationexception{
1. Create a factory
Documentbuilderfactory factory = Documentbuilderfactory.newinstance ();
2. Get DOM Parser
Documentbuilder builder = Factory.newdocumentbuilder ();
3. Parse the XML document and get the document that represents it
Document document = Builder.parse ("Webroot/product.xml");
}
Recursively print all the label (Element) names
@Test
public void Printtagname () throws Saxexception, IOException, parserconfigurationexception{
Documentbuilderfactory factory = Documentbuilderfactory.newinstance ();
Documentbuilder builder = Factory.newdocumentbuilder ();
Document document = Builder.parse ("Webroot/product.xml");
Get root node:
Node root = document.getElementById ("cata1");
Recursive invocation of the output node name and its child nodes
Printchildrennode (root);
}
private void Printchildrennode (node node) {
If the node is an element object, its element name is output, otherwise it will output #text
if (node instanceof Element) {
System.out.println ("<" +node.getnodename () + ">");
}
NodeList list = Node.getchildnodes ();
for (int i = 0; i < list.getlength (); i++) {
Node Childnode = List.item (i);
Printchildrennode (Childnode);
}
}
Get the value of the tag attribute in the XML document: <product category= "Handtool" >
@Test
public void Printtagattr () throws Exception {
Documentbuilderfactory factory = Documentbuilderfactory.newinstance ();
Documentbuilder builder = Factory.newdocumentbuilder ();
Document document = Builder.parse ("Webroot/product.xml");
String elementname = "Product";
String elementattrname = "Plant";
element element = (Element) document.getElementsByTagName (elementname). Item (0);
String Elementattrvalue = Element.getattribute (elementattrname);
System.out.println ("<" +elementname+ "" +elementattrname+ "= '" +elementattrvalue+ "' >");
}
}
where Product.xml file:
<?xml version= "1.0" encoding= "Utf-8"?> <! doctype catalog system "PRODUCT.DTD" > <catalog id= "cata1" > <product category= "Handtool" > < specifications weight= "2.0kg" > Wrench </specifications> < Price street= "Hong Kong Street" >80.0</price> <notes> This is a wrench </ notes> </product> <product category= "Table" > <specifications>&table;</specifications> <price street= "&street;" wholesale= "section" >&price;</price> </product> </catalog > |
The output of the Printtagname () method:
<catalog> <product> <specifications> <price> <notes> <product> <specifications> <price>
|
The output of the Printtagattr () method:
<product category= ' Handtool ' >
|