Getting to the root node of an xml document

Discussion in 'Java' started by Rodolphe G, Jun 1, 2007.

  1. Rodolphe G

    Rodolphe G Guest

    Hello there!

    I'm trying to get to an xml document root node (named dataroot), I've
    tried different things, like :

    Node node = doc.getFirstChild();
    Node node = doc.getDocumentElement();
    Node node = doc.getElementsByTagName("dataroot").item(0);

    None of them seem to work, the "node" variable is NULL everytime.

    Here's the start of the XML file, I somebody has an idea, it's be very
    helpful :) Cheers!

    XML Document :

    <?xml version="1.0" encoding="UTF-8"?>
    <dataroot xmlns:eek:d="urn:schemas-microsoft-com:eek:fficedata"
    generated="2007-05-25T20:41:06">
    <SITES>
    <FECD_ID>18</FECD_ID>
    <LATITUDE>55.53</LATITUDE>
    <LONGITUDE>-98.41</LONGITUDE>
    <SITE_INFORMATION>
    <FECD_ID>18</FECD_ID>
    <ORIGINAL_SOURCE>Halliwell &amp; Apps, 1997</ORIGINAL_SOURCE>
    <ORIGINAL_SOURCE_ID>MAN TE-OA (1)</ORIGINAL_SOURCE_ID>
    <ESTABLISHMENT_DATE>1994</ESTABLISHMENT_DATE>
    <PROVINCE>MB</PROVINCE>
    <TERRESTRIAL_ECOZONE>Boreal Shield</TERRESTRIAL_ECOZONE>
    <TERRESTRIAL_ECOREGION_TEXT>Churchill River Upland</
    TERRESTRIAL_ECOREGION_TEXT>
    <ORDER>Brunisolic</ORDER>
    <SUB_GROUP_GREAT_GROUP>Gleyed Melanic</SUB_GROUP_GREAT_GROUP>
    <DRAINAGE_CLASS_ID>Imperfectly</DRAINAGE_CLASS_ID>
    <FEATURE_DEPTH>33</FEATURE_DEPTH>
    <SOIL_FEATURE>Carbonates</SOIL_FEATURE>
    <ROOTING_DEPTH>53</ROOTING_DEPTH>
    <DOMINANT_TREE_1>POPU TRE</DOMINANT_TREE_1>
    <CODOMINANT_TREE_1>POPU TRE</CODOMINANT_TREE_1>
    ....

    Part of the code :

    public class WWXMLSitesLayer extends IconLayer implements
    SelectListener {

    public WWXMLSitesLayer(String fileName, String layerName)
    {
    this.setName(layerName);
    this.loadIcons(readXML(new File(fileName)), layerName);
    }

    // Load icons from XML document named layer
    private void loadIcons(Document doc, String layerName) {

    // Get LayerSet node
    // Node node = findLayerSetByName(doc, layerName);
    Node node = doc.getElementsByTagName("dataroot").item(0);

    if(node == null)
    return; // TODO: throw exception


    loadLayerSet(node);
    }

    // Load icons from a LayerSet node (recursive)
    private void loadLayerSet(Node node) {
    // Process icon child nodes
    Node child;

    for(int i = 0; i < node.getChildNodes().getLength(); i++) {
    // child = node.getChildNodes().item(i);
    child = node.getChildNodes().item(i);
    //if(child.getNodeName().compareToIgnoreCase("SITES") == 0) {
    // loadLayerSet(child); // recurse
    //}
    //else if(child.getNodeName().compareToIgnoreCase("SITES") ==
    0) {
    // Get icon info
    Node n;
    n = findChildByName(child, "SITES");
    String name = n.getTextContent();
    n = findChildByName(child, "LATITUDE");
    ....
    Rodolphe G, Jun 1, 2007
    #1
    1. Advertising

  2. Rodolphe G wrote:
    > Hello there!
    >
    > I'm trying to get to an xml document root node (named dataroot), I've
    > tried different things, like :
    >
    > Node node = doc.getFirstChild();
    > Node node = doc.getDocumentElement();
    > Node node = doc.getElementsByTagName("dataroot").item(0);
    >
    > None of them seem to work, the "node" variable is NULL everytime.


    Then the document is empty. Show us the code where you load it.
    Mike Schilling, Jun 1, 2007
    #2
    1. Advertising

  3. Rodolphe G

    Rodolphe G Guest

    Mike, that would be here :

    ----------------------------------------------------------------------------
    WWXMLSitesLayer("SITES.xml", "Sites d'├ętude")
    ----------------------------------------------------------------------------

    Here's the class:

    ----------------------------------------------------------------------------------------------------------------------------
    public class WWXMLSitesLayer extends IconLayer implements
    SelectListener {

    public WWXMLSitesLayer(String fileName, String layerName)
    {
    this.setName(layerName);
    this.loadIcons(readXML(new File(fileName)), layerName);
    }

    // Load icons from XML document named layer
    private void loadIcons(Document doc, String layerName) {

    // Get LayerSet node
    // Node node = findLayerSetByName(doc, layerName);
    Node node = doc.getElementsByTagName("dataroot").item(0);

    if(node == null)
    return; // TODO: throw exception


    loadLayerSet(node);
    }
    ....
    ----------------------------------------------------------------------------------------------------------------------------
    Rodolphe G, Jun 1, 2007
    #3
  4. Rodolphe G wrote:
    > Mike, that would be here :
    >
    > ----------------------------------------------------------------------------
    > WWXMLSitesLayer("SITES.xml", "Sites d'├ętude")
    > ----------------------------------------------------------------------------
    >
    > Here's the class:
    >
    > ----------------------------------------------------------------------------------------------------------------------------
    > public class WWXMLSitesLayer extends IconLayer implements
    > SelectListener {
    >
    > public WWXMLSitesLayer(String fileName, String layerName)
    > {
    > this.setName(layerName);
    > this.loadIcons(readXML(new File(fileName)), layerName);
    > }
    >
    > // Load icons from XML document named layer
    > private void loadIcons(Document doc, String layerName) {
    >
    > // Get LayerSet node
    > // Node node = findLayerSetByName(doc, layerName);
    > Node node = doc.getElementsByTagName("dataroot").item(0);
    >
    > if(node == null)
    > return; // TODO: throw exception
    >
    >
    > loadLayerSet(node);
    > }


    Presumably it's inside readXML(), then. At any rate, if doc is non-null but
    doc.getDocumentElement() is nul, you've got an empty document, which you
    wouldn't have if it were produced by parsing the XML file you showed.
    Mike Schilling, Jun 1, 2007
    #4
  5. Rodolphe G

    Rodolphe G Guest

    Ok, I'll go through it again and check if doc is indeed non-null since
    I know for a fact that my XML isn't :)
    Rodolphe G, Jun 1, 2007
    #5
    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,412
  2. Tony Prichard
    Replies:
    0
    Views:
    716
    Tony Prichard
    Dec 12, 2003
  3. Tjerk Wolterink
    Replies:
    2
    Views:
    1,404
    Dimitre Novatchev
    Aug 24, 2006
  4. Replies:
    1
    Views:
    554
    Martin Honnen
    May 8, 2007
  5. Lew
    Replies:
    3
    Views:
    325
    Rodolphe G
    Jun 2, 2007
Loading...

Share This Page