How to get value of XML "Element " type node

A

Amol

Hi,

I want to fetch some values from a XML file, using a java program.
I want to retrieve value of path tag for e.g<path>some path</path>
tag.(Marked with **** in the below XML file Example)

=============
My XML File
============

<maindata id="U00abfd8a-0ce7-ced5-00f6-00fff5bfdea9" host="e05k02"
addr="10.64.33.86" domain="BrioPortal 7.0 Development"
version="7.0.1.17" date="2003-08-04 14:20:23-07">
<Category name="MigSMACT" version="7.0.1.17"
id="U01929031-0e01-ced5-00f6-00fff5bfdea9">
<gendate>2003-08-04 14:20:25-07</gendate>
<cattribs>
<autodelete>false</autodelete>
<browsable>true</browsable>
<datecreated>2003-07-28&sp;14:35:33-07</datecreated>
<description>New&sp;Category</description>
<group name="ADMINISTRATOR"
idref="U0159c5ff-13e4-ced5-00f6-00fff5bfdea9"/>
<lastmodified>2003-07-28&sp;14:35:33-07</lastmodified>
<name>MigSMACT</name>
<objectid>HXLWIRBZFXECHBDYAGHLIRRPZe0Ck0N-BCRRP_Z0FK0N</objectid>
<objecttype mimetype="Category"
idref="U00d5f038-13e8-ced5-00f6-00fff5bfdea9"/>
<owner name="dw1portl"
idref="U0057d3f5-143b-ced5-00f6-00fff5bfdea9"/>
<parentcategory name="Migration Staging"
idref="U001c7c0d-1456-ced5-00f6-00fff5bfdea9"/>
<parentobjectid>FBRGZLXIDHCITIDYG0R0LLP_eZFkZC-FCLRB_E0FM0C</parentobjectid>
****** <path>/ABC&sp;Shipping/Goods&sp;Sale/Final</path> *******
<permissions>
<Permissions>
<groupexecutepermission>true</groupexecutepermission>
<groupreadpermission>true</groupreadpermission>
<groupwritepermission>true</groupwritepermission>
<ownerexecutepermission>true</ownerexecutepermission>
<ownerreadpermission>true</ownerreadpermission>
<ownerwritepermission>true</ownerwritepermission>
<worldexecutepermission>true</worldexecutepermission>
<worldreadpermission>true</worldreadpermission>
<worldwritepermission>true</worldwritepermission>
</Permissions>
</permissions>
</cattribs>
</Category>

==============
My Java Code
=============

DOMParser tParser = new DOMParser();
tParser.parse("WRTest_11-28-5:38_sdw-sb.xml");
Document doc = tParser.getDocument();

doc.getDocumentElement ();
System.out.println ("Root element :" +
doc.getDocumentElement().getNodeName());

NodeList objCatNodes = doc.getElementsByTagName("path");

int totalCatNodes = objCatNodes.getLength();
System.out.println("Total no of Path tags: " + totalCatNodes);

Node objNode=objCatNodes.item(0);
System.out.println("Path Name : " + objNode.getNodeName());

-------------------------------------
In the last System.out.println, it is returning NULL.

Please tell me what I need to do, to get the value. Plz help me its
Urgent. thanks in advance.

Amy..........
 
F

Finnbarr P. Murphy

The following will give you what you are looking for:


import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.*;

public class amol {

public static void main(String[] args) {

try {
DOMParser tParser = new DOMParser();

tParser.parse(args[0]);
Document doc = tParser.getDocument();
doc.getDocumentElement();
System.out.println ("Root element :" +
doc.getDocumentElement().getNodeName());

NodeList objCatNodes = doc.getElementsByTagName("path");

int totalCatNodes = objCatNodes.getLength();
System.out.println("Total no of Path tags: " + totalCatNodes);

Node objNode=objCatNodes.item(0);

NodeList objNodes = objNode.getChildNodes();
System.out.println("Path Name : " + objNodes.item(0).getNodeValue());

} catch (Exception e) {e.printStackTrace();}
}

}

Enjoy!

- Finnbarr
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top