How to get value of XML "Element " type node

Discussion in 'XML' started by Amol, Nov 29, 2003.

  1. Amol

    Amol Guest

    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..........
     
    Amol, Nov 29, 2003
    #1
    1. Advertising

  2. 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


    (Amol) wrote in message news:<>...
    > 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..........
     
    Finnbarr P. Murphy, Dec 2, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    1,496
  2. Chishun Kwong
    Replies:
    0
    Views:
    421
    Chishun Kwong
    Mar 3, 2005
  3. Tjerk Wolterink
    Replies:
    2
    Views:
    1,443
    Dimitre Novatchev
    Aug 24, 2006
  4. HANM
    Replies:
    2
    Views:
    729
    Joseph Kesselman
    Jan 29, 2008
  5. JB
    Replies:
    4
    Views:
    969
    Juan T. Llibre
    Apr 8, 2008
Loading...

Share This Page