开发者

Java/Jsp XML Dom

开发者 https://www.devze.com 2022-12-21 12:48 出处:网络
looks like I need help again! :-/ Im trying to read this XML file with Java/Jsp: <?xml version=\"1.0\" encoding=\"iso-8859-1\"?>

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>
        </schedul开发者_如何转开发e_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.... :-/


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消