简体   繁体   English

从节点列表获取第4个节点-domparser Java

[英]Get 4th node from nodelist - domparser java

The value getting sent for execType is blank. execType发送的值是空白。 The url Im using is fine, but the value being outputted for this is blank: System.out.println("Program title is: " + showTitleAttr); Im使用的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();
        }
 }

the xml Im trying to parse, looks like: 我试图解析的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>

Im basically trying to get the value of the 4th element "programTitle" from the root. 我基本上是试图从根获得第四个元素“ programTitle”的值。

If anyone can help me, I am very new to Domparser. 如果有人可以帮助我,我对Domparser还是陌生的。 Thank you! 谢谢!

Since no one was able to help me, I figured this out on my own. 由于没有人能够帮助我,所以我自己解决了这个问题。 I hope this helps others who may be trying to traverse an xml document: 我希望这对可能正在遍历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();
        }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM