我正在从XML文件中解析文本和图像。 文字很好。 但是当我开始解析图像时,listview变空了。
for (int j = 0; j < clength; j++) {
Node thisNode = nchild.item(j);
String theString = null;
String nodeName = null;
if (thisNode != null && thisNode.getFirstChild() != null) {
theString = thisNode.getFirstChild().getNodeValue();
}
nodeName = thisNode.getNodeName();
if ("title".equals(nodeName)) {
_item.setTitle(theString);
}
if ("pubDate".equals(nodeName)) {
String formatedDate = theString.replace(" +0000", "");
_item.setDate(formatedDate);
}
if (nodeName.equals("enclosure")) {
Element enclosure = (Element) thisNode;
if (enclosure.hasAttr("url")) {
String imageLink = (enclosure.attr("url"));
_item.setImage(imageLink);
}
}
}
所以,据我所知,问题是从中获取数据。 我该如何以正确的方式做到这一点? 日Thnx!
答案 0 :(得分:0)
所以,我通过这种方式解决了这个问题。
String body = nodeToString(thisNode);
int urlStart = body.indexOf("url=");
int urlFinish = body.indexOf("length");
String urlImage = body.substring(urlStart+5, urlFinish-2);
_item.setImage(urlImage);
方法nodeToString:
private String nodeToString(Node node) {
StringWriter sw = new StringWriter();
try {
Transformer t = TransformerFactory.newInstance().newTransformer();
t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
t.transform(new DOMSource(node), new StreamResult(sw));
} catch (TransformerException te) {
System.out.println("nodeToString Transformer Exception");
}
return sw.toString();
}