DOM parsing-readxml operations on XML using XML parsing development package JAXP

Source: Internet
Author: User

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 ' >


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.