gpt4 book ai didi

Java/Jsp XML Dom

转载 作者:行者123 更新时间:2023-12-01 19:24:12 26 4
gpt4 key购买 nike

看来我又需要帮助了! :-/

我正在尝试使用 Java/Jsp 读取此 XML 文件:

<?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>

这是我的 Java/Jsp 代码:

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 />";
}
}

}

但是当我输出字符串资源时,我得到:

#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:

我不明白... getNodeName() 如何返回节点的名称,但 getNodeValue() 返回 null....?请帮助我,在发布到这里之前我已经做了很多搜索和失败的尝试,但没有任何效果......:-/

最佳答案

事实上,您获得的节点不是文本节点本身。这是一个Element并且 getNodeValue 将返回 null(参见表 here )。您必须使用 getTextContent相反。

关于Java/Jsp XML Dom,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2362732/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com