gpt4 book ai didi

java - 如何为 <![CDATA[]]> 解析 XML

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:42:19 38 4
gpt4 key购买 nike

如何解析包含数据的 XML <![CDATA[---]...我们如何解析 xml 并获取包含在 CDATA 中的数据? ???

最佳答案

public static void main(String[] args) throws Exception {
File file = new File("data.xml");
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
//if you are using this code for blackberry xml parsing
builder.setCoalescing(true);
Document doc = builder.parse(file);

NodeList nodes = doc.getElementsByTagName("topic");
for (int i = 0; i < nodes.getLength(); i++) {
Element element = (Element) nodes.item(i);
NodeList title = element.getElementsByTagName("title");
Element line = (Element) title.item(0);
System.out.println("Title: " + getCharacterDataFromElement(line));
}
}
public static String getCharacterDataFromElement(Element e) {
Node child = e.getFirstChild();
if (child instanceof CharacterData) {
CharacterData cd = (CharacterData) child;
return cd.getData();
}
return "";
}

( http://www.java2s.com/Code/Java/XML/GetcharacterdataCDATAfromxmldocument.htm )

关于java - 如何为 &lt;![CDATA[]]> 解析 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8489151/

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