我正在尝试读取XML文件,并将标签之间的值存储为String。但是对于一个标签<picture url="https://cloudstor.aarnet.edu.au/plus/s/62e0uExNviPanZE/download"/>
我需要获取图像的URL并存储它,以便将来使用。
我用过
File stocks = new File("gui.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
dbFactory.setIgnoringElementContentWhitespace(true);
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(stocks);
doc.getDocumentElement().normalize();
System.out.println("root of xml file" + doc.getDocumentElement().getNodeName());
NodeList nodes = doc.getElementsByTagName("billboard");
System.out.println("==========================");
Node node = nodes.item(0);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
//message = getValue("message", element);
information = getValue("information", element).trim();
String picture = getValue("picture url=", element);
}
private static String getValue(String tag, Element element) {
NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes();
Node node = (Node) nodes.item(0);
return node.getNodeValue();
}
这将有助于从普通标签中获取信息,例如此处的信息标签。知道如何使它适用于图片标签。
<billboard class="nodarken">
<information>
Billboard with an information tag and nothing else. Note that the text is word-wrapped. The quick brown fox jumped over the lazy dogs.
</information>
<picture url="https://cloudstor.aarnet.edu.au/plus/s/62e0uExNviPanZE/download"/>
</billboard>
<billboard class="nodarken">
<information>
Billboard with an information tag and nothing else. Note that the text is word-wrapped. The quick brown fox jumped over the lazy dogs.
</information>
<picture><![CDATA[ https://cloudstor.aarnet.edu.au/plus/s/62e0uExNviPanZE/download"]]></picture>
</billboard>