0

发送的值execType 是空白的。我使用的 url 很好,但是为此输出的值是空白的:System.out.println("Program title is: " + showTitleAttr);

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 看起来像:

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>

我基本上试图从根中获取第四个元素“programTitle”的值。

如果有人可以帮助我,我对 Domparser 很陌生。谢谢!

4

1 回答 1

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();
        }
 }
于 2013-06-18T13:31:32.367 回答