1 Reply Latest reply: Apr 22, 2009 1:56 AM by 807588 RSS

    Can't retrieve sub element in xml

    807588
      I'm reading from an xml document which has gone fine up until now. There are two tags with the exact same element name, only one is a sub and one is in the main tree. I am trying to read the main one, but for some reason the sub is getting called. Is there a way to make java go to the next tag with that name? The root is actually "feed", but I set the NodeList thing to "entry" as that's after the subtree, but still doesn't work.
      Here is my code:
      DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
      DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
      Document doc = dBuilder.parse(URL1);
      doc.getDocumentElement().normalize();
      NodeList nList = doc.getElementsByTagName("entry");
      for (int temp = 0; temp < nList.getLength(); temp++) {
      Node nNode = nList.item(temp);
      if (nNOde.getNodeType() == Node.ELEMENT_NODE) {
      Element eElement = (Element) nNode;
      URL2 = (getTagValue("id",eElement));
      }
      }
      Here is the XML document so you can see how the tree is set up. I need to retrieve the address at "id" to go to another xml document.
      <?xml version = '1.0' encoding = 'UTF-8' standalone = 'yes'?>
      <?xml-stylesheet href='capatom.xsl' type='text/xsl'?>
      
      <!--
      this comment is here to foil Internet Explorer and Firefox attempt to treat
      this as a channel and thereby ignore our stylesheet
      this comment is here to foil Internet Explorer and Firefox attempt to treat
      this as a channel and thereby ignore our stylesheet
      this comment is here to foil Internet Explorer and Firefox attempt to treat
      this as a channel and thereby ignore our stylesheet
      -->
      <feed xmlns = 'http://www.w3.org/2005/Atom'
      xmlns:cap = 'urn:oasis:names:tc:emergency:cap:1.1'
      xmlns:ha = 'http://www.alerting.net/namespace/index_1.0'>
      
        <id>http://www.weather.gov/alerts-beta/wwaatmget.php?x=NDZ025</id>
        <generator>
        NWS CAP Server
        </generator>
        <updated>2009-04-22T01:45:37-04:00</updated>
      
        <author>
        <name>
        w-nws.webmaster@noaa.gov
        </name>
        </author>
        <title>
        Current Watches, Warnings and Advisories for Foster (NDZ025) North Dakota Issued by the National Weather Service
        </title>
        <link href='http://www.weather.gov/alerts-beta/wwaatmget.php?x=NDZ025'/>
      <entry>
      
      <id>http://www.weather.gov/alerts-beta/wwacapget.php?x=ND20090421154400BISFloodWarningBIS20090422180000ND</id>
      <updated>2009-04-22T01:45:37-04:00</updated>
      <author>
      <name>
      w-nws.webmaster@noaa.gov
      </name>
      </author>
      <title>
      Flood Warning issued April 21, 2009 at 10:44AM CDT expiring April 22, 2009 at 1:00PM CDT
      Issued by Weather Forecast Office Bismark http://www.crh.noaa.gov/bis/
      </title>
      <link href="http://www.weather.gov/alerts-beta/wwacapget.php?x=ND20090421154400BISFloodWarningBIS20090422180000ND"/>
      <summary>
      ...THE FLOOD WARNING CONTINUES FOR THE FOLLOWING RIVERS IN NORTHDAKOTA...  JAMES RIVER NEAR GRACE CITY AFFECTING FOSTER COUNTY  JAMES RIVER AT LAMOURE AFFECTING LA MOURE COUNTY  PIPESTEM CREEK NEAR PINGREE AFFECTING STUTSMAN COUNTY...THE FLOOD WARNING CONTINUES FOR THE FOLLOWING RIVERS IN NORTH
      </summary>
      <cap:effective>
      
      2009-04-21T10:44:00-05:00
      </cap:effective>
      <cap:expires>
      2009-04-22T13:00:00-05:00
      </cap:expires>
      <cap:status>
      Actual
      </cap:status>
      <cap:msgType>
      Alert
      </cap:msgType>
      <cap:category>
      Met
      </cap:category>
      <cap:urgency>
      Expected
      </cap:urgency>
      
      <cap:severity>
      Moderate
      </cap:severity>
      <cap:certainty>
      Likely
      </cap:certainty>
      <cap:areaDesc>
      Foster
      </cap:areaDesc>
      </entry>
      <entry>
      <id>http://www.weather.gov/alerts-beta/wwacapget.php?x=ND20090421200200BISFloodWarningBIS20090422200000ND</id>
      <updated>2009-04-22T01:45:37-04:00</updated>
      <author>
      <name>
      
      w-nws.webmaster@noaa.gov
      </name>
      </author>
      <title>
      Flood Warning issued April 21, 2009 at 3:02PM CDT expiring April 22, 2009 at 3:00PM CDT
      Issued by Weather Forecast Office Bismark http://www.crh.noaa.gov/bis/
      </title>
      <link href="http://www.weather.gov/alerts-beta/wwacapget.php?x=ND20090421200200BISFloodWarningBIS20090422200000ND"/>
      <summary>
      THE NATIONAL WEATHER SERVICE IN BISMARCK HAS ISSUED A* FLOOD WARNING FOR...  BOTTINEAU COUNTY IN NORTH CENTRAL NORTH DAKOTA...  MCHENRY COUNTY IN NORTH CENTRAL NORTH DAKOTA...  PIERCE COUNTY IN NORTH CENTRAL NORTH DAKOTA...  ROLETTE COUNTY IN NORTH CENTRAL NORTH DAKOTA...
      </summary>
      <cap:effective>
      2009-04-21T15:02:00-05:00
      </cap:effective>
      <cap:expires>
      2009-04-22T15:00:00-05:00
      </cap:expires>
      <cap:status>
      
      Actual
      </cap:status>
      <cap:msgType>
      Alert
      </cap:msgType>
      <cap:category>
      Met
      </cap:category>
      <cap:urgency>
      Expected
      </cap:urgency>
      <cap:severity>
      Moderate
      </cap:severity>
      <cap:certainty>
      Likely
      </cap:certainty>
      
      <cap:areaDesc>
      Bottineau; Burleigh; Dickey; Emmons; Foster; Kidder; La Moure; Logan; McHenry; McLean; Mercer; Mountrail; Pierce; Rolette; Sheridan; Stutsman; Ward; Wells
      </cap:areaDesc>
      </entry>
      </feed>