tags:

views:

49

answers:

2

Hi all, looks like I need help again! :-/

Im trying to read this XML file with Java/Jsp:

<?xml version="1.0" encoding="iso-8859-1"  ?>
<universal_campaign>
    <campaign_details>
        <campaign_id></campaign_id>
        <campaign_title>Test Campaign</campaign_title>
        <campaign_sdate>2010-01-21</campaign_sdate>
        <campaign_edate>2010-01-25</campaign_edate>
        <campaign_priority>Normal</campaign_priority>
    </campaign_details>

    <campaign_schedule>
        <schedule_sdate>2010-01-25</schedule_sdate>
        <schedule_edate>2010-01-30</schedule_edate>
        <schedule_priority>Normal</schedule_priority>

        <schedule_content>
            <content_name>Wallpaper_A</content_name>
            <content_filename>WP_A.jpg</content_filename>
        </schedule_content>

        <schedule_content>
            <content_name>Screensaver</content_name>
            <content_filename>SCS.gif</content_filename>
        </schedule_content>


        <schedule_zone>universal.001 test 001</schedule_zone>
        <schedule_zone>universal.001 test 002</schedule_zone>
        <schedule_zone>universal.001 test 003</schedule_zone>
    </campaign_schedule>

</universal_campaign>

Here is my Java/Jsp code:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(fileName);
NodeList nl, nl2;
NodeList campaign_details = doc.getElementsByTagName("universal_campaign");
String res = "";

for(int i = 0;i<campaign_details.getLength();i++){

  nl = campaign_details.item(i).getChildNodes();

  for(int j = 0; j<nl.getLength();j++){                            
    nl2 = nl.item(j).getChildNodes();

      for(int k = 0; k<nl2.getLength();k++){
        res += nl2.item(k).getNodeName()+": "+nl2.item(k).getNodeValue()+"<br />";
      }
   }

}

But when I output the String res, I get:

#text: 
campaign_id: null
#text: 
campaign_title: null
#text: 
campaign_sdate: null
#text: 
campaign_edate: null
#text: 
campaign_priority: null
#text: 
#text: 
schedule_sdate: null
#text: 
schedule_edate: null
#text: 
schedule_priority: null
#text: 
schedule_content: null
#text: 
schedule_content: null
#text: 
schedule_zone: null
#text: 
schedule_zone: null
#text: 
schedule_zone: null
#text: 

And I don't get it... how can the getNodeName() return the name of the node, but the getNodeValue() returns null....? Please help me, I have done many search and failed attempts before posting here, but nothing worked.... :-/

+2  A: 

Use getTextContent() or, even better, use a better XML API (jdom springs to mind).

Martin Wickman
Thanks it worked! but I still have the "#text:" appearing in my output... why?I will check jdom out! Thanks for the advice.
Piero
+2  A: 

The fact is that the node you're getting is not the text node itself. It's an Element and getNodeValue will return null (see table here). You'll have to use getTextContent instead.

Valentin Rocher
Thanks it worked! but I still have the "#text:" appearing in my output... why?
Piero
Because of the line returns in your XML. The space is treated as a text node with nothing in it except blank characters. So if you know which elements you want, you'd better only filter on these elements.
Valentin Rocher
Ah ok thanks you very much for your time!
Piero