Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > How to get value of XML "Element " type node

Reply
Thread Tools

How to get value of XML "Element " type node

 
 
Amol
Guest
Posts: n/a
 
      11-29-2003
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..........
 
Reply With Quote
 
 
 
 
Finnbarr P. Murphy
Guest
Posts: n/a
 
      12-02-2003
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


http://www.velocityreviews.com/forums/(E-Mail Removed) (Amol) wrote in message news:<(E-Mail Removed). com>...
> 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..........

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
<need help>How to print the preceding node value based on descendant node in XSLT? njsimha XML 0 09-16-2008 12:33 PM
xsl variable $node/text() but $node can non-node-set help! Tjerk Wolterink XML 2 08-24-2006 03:28 AM
How to set the node indent property between the parent node and the leaf node viveknatani@gmail.com ASP .Net 0 02-13-2006 07:11 PM
Select Node Using position or value of another node. Eddy C XML 6 10-17-2005 11:02 PM
XSLT: branching node processing with respect to node type possible? Ralf Wahner XML 2 07-15-2003 04:18 PM



Advertisments