2013-06-17 3 views
0

Значение, отправленное для execType, пустое. URL Im используя это хорошо, но значение того, выводимый для этого является пустым: System.out.println("Program title is: " + showTitleAttr);Получить 4-й узел от нодлиста - domparser java

protected static String getNodeAttr(String tagName, String attrName, NodeList nodes) { 
    for (int x = 0; x < nodes.getLength(); x++) { 
     Node node = nodes.item(x); 
     if (node.getNodeName().equalsIgnoreCase(tagName)) { 
      NodeList childNodes = node.getChildNodes(); 
      for (int y = 0; y < childNodes.getLength(); y++) { 
       Node data = childNodes.item(y); 
       if (data.getNodeType() == Node.ATTRIBUTE_NODE) { 
        if (data.getNodeName().equalsIgnoreCase(attrName)) 
         return data.getNodeValue(); 
       } 
      } 
     } 
    } 

    return ""; 
} 

public static void main(String args[]) { 
     try 
     { 
      DOMParser parser = new DOMParser(); 
      String UrlToParse = "theurl"; 
      parser.parse(UrlToParse); 
      Document doc = parser.getDocument(); 

      // Get the document's root XML node 
      NodeList root = doc.getChildNodes(); 

      // Navigate down the hierarchy to get to the program node 
      Node comp = getNode("ProgramGuideWCSResponse", root); 
      Node exec = getNode("programGuide", comp.getChildNodes()); 
      String execType = getNodeAttr("programTitle", exec); 

      // Load the executive's data from the XML 
      NodeList nodes = exec.getChildNodes(); 
      String showTitleAttr = getNodeValue("programTitle", nodes); 

      System.out.println("Program title is: " + showTitleAttr); 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
} 

XML-Im пытаясь разобрать, выглядит следующим образом:

ProgramGuideWCSResponse xmlns="urn:com:X:presentationflow:spps:services:programguide"> 
    <programGuide xmlns=""> 
    <startDate>2013-06-17-04:00</startDate> 
    <endDate>2013-06-23-04:00</endDate> 
    <locale>en_US</locale> 
    <programs> 
     <program> 
     <programCode>XOA</programCode> 
     <programTitle>The Oreck Challenge/Select Comfort Sleep Number</programTitle> 
     <positioningStatement>Can your current vacuum pick up a bowling ball? The Oreck can! Look for it and other powerful cleaning solutions including air cleaners, deodorizers, steam cleaners and more, all with the strength and quality you'd expect from Oreck, one of the best-known and trusted names in the industry.~ Imagine a great night's sleep on a mattress that allows you to customize its firmness. Sound too good to be true? Not with the Select Comfort Personal Preference Mattress System, a mattress that lets you adjust each side of the bed separately via its unique air chambers. Select Comfort has created these mattresses especially for X, so we can bring you prices you won't lose sleep over.</positioningStatement> 
     <occurrences> 
      <scheduledProgram> 
      <startDate>2013-06-17-04:00</startDate> 
      <startTime>13:00:00.000-04:00</startTime> 
      <sourceCode>13061713</sourceCode> 
      <durationHours>1.0</durationHours> 
      <newShow>false</newShow> 
      <deferredPay>false</deferredPay> 
      <events/> 
      </scheduledProgram> 
     </occurrences> 
     </program> 
    </programs> 
    </programGuide> 
</ProgramGuideWCSResponse> 

Im в основном пытаются получить значение 4-й элемент «programTitle» из корня.

Если кто-нибудь может мне помочь, я очень новичок в Domparser. Спасибо!

ответ

0

Поскольку никто не смог мне помочь, я понял это сам. Надеюсь, это поможет другим людям, которые могут пытаться пройти XML-документ:

public static void main(String args[]) { 
     try { 
      DOMParser parser = new DOMParser(); 
      String UrlToParse = "puttheurl.com"; 
      parser.parse(UrlToParse); 
      Document doc = parser.getDocument(); 

      // Get the document's root XML node 
      NodeList ProgramGuideRoot = doc.getChildNodes(); 

      // Navigate down the hierarchy to get to the program node 
       // Starting with Program Guide Root -> Program Guide -> All Programs -> Individual Program 
      Node PGR = getNode("ProgramGuideWCSResponse", ProgramGuideRoot); 
      Node PG = getNode("programGuide", PGR.getChildNodes()); 
      Node Programs = getNode("programs", PG.getChildNodes()); 
      Node IndivdualProgram = getNode("program", Programs.getChildNodes()); 

      // Load the executive's data from the XML 
      NodeList nodes = IndivdualProgram.getChildNodes(); 
      String showTitleAttr = getNodeValue("programTitle", nodes); 

      System.out.println("Program title is: " + showTitleAttr); 
     } 
     catch (Exception e) { 
      e.printStackTrace(); 
     } 
}