Domanda

The value getting sent for execType is blank. The url Im using is fine, but the value being outputted for this is blank: 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:

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.

If anyone can help me, I am very new to Domparser. Thank you!

È stato utile?

Soluzione

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:

 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();
        }
 }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top