Use Dom to parse XML files

Source: Internet
Author: User

In the past, I used to parse XML files. I thought it was simple to parse the files just to implement the results. In yesterday's project, we had to parse the XML file and found that we didn't go deep into it. Calm down and have a good look at the book, which has benefited a lot. Below are several common examples to illustrate the basic usage of Dom.

 

Referenced XML file (borrowed. XML ):

<? XML version = "1.0" encoding = "gb2312"?>
<Newdataset xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: nonamespaceschemalocation = "C: \ sourcelist \ borrowed. XSD">
<Record>
<Bdate> 2001-06-18 00:00:00. 0 </bdate>
<Bpieces> 0 </bpieces>
<DNO> 00020 </DNO>
<MnO> 0001 </MnO>
<Rdate> 00:00:00. 0 </rdate>
<Rno> 01 1 </rno>
<Status> 1 </status>
</Record>
<Record>
<Bdate> 2001-06-18 00:00:00. 0 </bdate>
<Bpieces> 0 </bpieces>
<DNO> 00053 </DNO>
<MnO> 0001 </MnO>
<Rdate> 00:00:00. 0 </rdate>
<Rno> 01 2 </rno>
<Status> 1 </status>
</Record>
<Record>
<Bdate> 2001-06-18 00:00:00. 0 </bdate>
<Bpieces> 0 </bpieces>
<DNO> 00086 </DNO>
<MnO> 0004 </MnO>
<Rdate> 00:00:00. 0 </rdate>
<Rno> 01 3 </rno>
<Status> 1 </status>
</Record>
<Record>
<Bdate> 2001-06-18 00:00:00. 0 </bdate>
<Bpieces> 0 </bpieces>
<DNO> 00053 </DNO>
<MnO> 0001 </MnO>
<Rdate> 00:00:00. 0 </rdate>
<Rno> 01 2 </rno>
<Status> 1 </status>
</Record>

</Newdataset>

 

1. Get the XML file version and character format

/**
* Get the XML file version and character format
* Document. getxmlversion ()
* Document. getxmlencoding ()
*/
Public void get_versionandencoding ()
{
Try
{
Documentbuilderfactory factory = documentbuilderfactory. newinstance ();
Documentbuilder builder = factory. newdocumentbuilder ();
Document document = builder. parse (new file ("borrowed. xml "));

String version = Document. getxmlversion ();
System. Out. println ("the XML file version is:" + version );
String encoding = Document. getxmlencoding ();
System. Out. println ("XML file encoding:" + encoding );
} Catch (exception e) {e. printstacktrace ();}
}

2. Get the header node of the XML file

/**
* Get the header node of the XML file
* Document. getdocumentelement (). getnodename ()
*/
Public void get_rootname ()
{
Try
{
Documentbuilderfactory factory = documentbuilderfactory. newinstance ();
Documentbuilder builder = factory. newdocumentbuilder ();
Document document = builder. parse (new file ("borrowed. xml "));

Element root = Document. getdocumentelement ();
String rootname = root. getnodename ();
System. Out. println ("Name of the root node of the XML file:" + rootname );
} Catch (exception e) {e. printstacktrace ();}
}

 

3. Obtain the child nodes of the header node of the XML file and related content of the child nodes.

Method 1:

/**
* Obtain the child nodes of the header node of the XML file and related content of the child nodes.

* Document. getelementsbytagname ("record ")

* Nodelist

* Item ()
*/
Public void get_root_childnameandcontent ()
{
Try
{
Documentbuilderfactory factory = documentbuilderfactory. newinstance ();
Documentbuilder builder = factory. newdocumentbuilder ();
Document document = builder. parse (new file ("borrowed. xml "));

Nodelist = Document. getelementsbytagname ("record ");
Int size = nodelist. getlength ();
For (INT I = 0; I <size; I ++)
{
Node node = nodelist. item (I );
String name = node. getnodename ();
String content = node. gettextcontent ();
System. Out. println (name );
System. Out. println ("" + content );
}
} Catch (exception e) {e. printstacktrace ();}
}

 

Method 2:

/**
* Obtain the child nodes of the XML file header node and related content of the child nodes.

* Nodelist = root. getchildnodes ();

* Element elementnode = (element) node
*/
Public void get_root_childnameandcontent ()
{
Try
{
Documentbuilderfactory factory = documentbuilderfactory. newinstance ();
Documentbuilder builder = factory. newdocumentbuilder ();
Document document = builder. parse (new file ("borrowed. xml "));

Element root = Document. getdocumentelement ();
Nodelist = root. getchildnodes ();
Int size = nodelist. getlength ();
System. Out. println (size );
For (INT I = 0; I <size; I ++)
{
Node node = nodelist. item (I );
If (node. getnodetype () = node. element_node)
{
Element elementnode = (element) node;
String name = elementnode. getnodename ();
String content = elementnode. gettextcontent ();
System. Out. println (name + "\ n" + content );
}
}
} Catch (exception e) {e. printstacktrace ();}
}

 

4. Get the child node name of the XML file header Node

 

/**
* Get the child node name of the XML file header Node

* Node. getnodetype () = node. element_node
* Element elementnode = (element) node
*/
Public void get_root_childname ()
{
Try
{
Documentbuilderfactory factory = documentbuilderfactory. newinstance ();
Documentbuilder builder = factory. newdocumentbuilder ();
Document document = builder. parse (new file ("borrowed. xml "));

Element root = Document. getdocumentelement ();
Nodelist = root. getchildnodes ();
Int size = nodelist. getlength ();
System. Out. println (size );
For (INT I = 0; I <size; I ++)
{
Node node = nodelist. item (I );
If (node. getnodetype () = node. element_node)
{
Element elementnode = (element) node;
String name = elementnode. getnodename ();
System. Out. println (name );
}
}
} Catch (exception e) {e. printstacktrace ();}
}

 

5. Get the name and value of the Sun Tzu node and generate a string in XML format to facilitate table display.

/**
* Get the name of the Sun Tzu Node

* Header
*/
Public String get_root_childname_childname ()
{
String child_childname = "";
Stringbuffer nodename = new stringbuffer ();
Try
{
Documentbuilderfactory factory = documentbuilderfactory. newinstance ();
Documentbuilder builder = factory. newdocumentbuilder ();
Document document = builder. parse (new file ("borrowed. xml "));

Element root = Document. getdocumentelement ();
Nodelist child = root. getelementsbytagname ("record ");
Int size = Child. getlength ();
System. Out. println (size );
Nodename. append ("<? XML version = \ "1.0 \" encoding = \ "gb2312 \"?> ");
Nodename. append ("\ n <Table border = 1> ");
Node node = Child. Item (1 );
If (node. getnodetype () = node. element_node)
{
Element elementnode = (element) node. getchildnodes ();
Nodelist elementnodechild = elementnode. getchildnodes ();
Int size1 = elementnodechild. getlength ();
Nodename. append ("\ n <tr> ");
For (Int J = 0; j <size1; j ++)
{
Node node1 = elementnodechild. Item (j );
If (node1.getnodetype () = node. element_node)
{
Element elementnode1 = (element) node1.getchildnodes ();
String name = elementnode1.getnodename ();

Nodename. append ("\ n <TH> ");
Nodename. append (name );
Nodename. append ("</Th> ");
}
}
Nodename. append ("\ n </tr> ");
}
String values = getnodevalue (Root );
Nodename. append (values );
Nodename. append ("\ n </table> ");
Child_childname = nodename. tostring ();
} Catch (exception e) {e. printstacktrace ();}
Return child_childname;
}

 

/**
* Get the value of the Sun Tzu Node

* Table content
*/
Public String getnodevalue (element root)
{
String nodevalue = "";
Stringbuffer nodevalues = new stringbuffer ();
Nodelist child = root. getelementsbytagname ("record ");
Int size = Child. getlength ();
System. Out. println (size );

For (INT I = 0; I <size; I ++)
{
Node node = Child. item (I );
If (node. getnodetype () = node. element_node)
{
Element elementnode = (element) node. getchildnodes ();
Nodelist elementnodechild = elementnode. getchildnodes ();
Int size1 = elementnodechild. getlength ();
Nodevalues. append ("\ n <tr> ");
For (Int J = 0; j <size1; j ++)
{
Node node1 = elementnodechild. Item (j );
If (node1.getnodetype () = node. element_node)
{
Element elementnode1 = (element) node1.getchildnodes ();
String content = elementnode1.gettextcontent ();
Nodevalues. append ("\ n <TD> ");
Nodevalues. append (content );
Nodevalues. append ("</TD> ");
}
}
Nodevalues. append ("\ n </tr> ");
}
}
Nodevalue = nodevalues. tostring ();
Return nodevalue;
}

 

The preceding operations only list a few simple XML files and will be supplemented later.

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.