我想解析一個DOM元素。使用Java解析XML
元素元件:
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<id>http://X/feed2</id>
<title>Sample Feed</title>
<entry>
<id>http://X/feed2/104</id>
<title>New Title</title>
</entry>
</feed>
我試圖取以下條目:
<entry>
<id>http://top.cs.vt.edu/libx2/[email protected]/feed2/104</id>
<title>New Title</title>
</entry>
我通過使用XPath解析XML:
「/原子:進料/ atom:entry [atom:id = \「http:// X/feed2/104 \」]「
但是,當我試圖解析Dom時出現異常元件。有人可以建議一個簡單的方法來實現這個在Java?
請參閱我的全碼:
public static parseXml() {
String externalEntryIdUrl = "http://theta.cs.vt.edu/~rupen/thirtylibapps/137";
String externalFeedUrl = StringUtils.substringBeforeLast(externalEntryIdUrl, "/");
try {
URL url = new URL(externalFeedUrl);
InputStream externalXml = new BufferedInputStream(url.openStream());
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(externalXml);
Element externalFeed = doc.getDocumentElement();
String atomNameSpace = "xmlns:atom=\"http://www.w3.org/2005/Atom\"";
String entryIdPath = String.format("//%s:entry[%s:id=%s]", atomNameSpace, atomNameSpace, externalEntryIdUrl);
Element externalEntry = (Element) XPathSupport.evalNode(entryIdPath, externalFeed);
} catch (Exception ex) {
// Throw exception
}
}
static synchronized Node evalNode(String xpathExpr, Node node) {
NodeList result = evalNodeSet(xpathExpr, node);
if (result.getLength() > 1)
throw new Error ("More than one node for:" + xpathExpr);
else if (result.getLength() == 1)
return result.item(0);
else
return null;
}
static synchronized NodeList evalNodeSet(String xpathExpr, Node node) {
try {
static XPath xpath = factory.newXPath();
xpath.setNamespaceContext(context);
static NamespaceContext context = new NamespaceContext() {
private Map<String, String> prefix2URI = new HashMap<String, String>();
{
prefix2URI.put("libx", "http://libx.org/xml/libx2");
prefix2URI.put("atom", "http://www.w3.org/2005/Atom");
}
};
XPathExpression expr = xpath.compile(xpathExpr);
Object result = expr.evaluate(node, XPathConstants.NODESET);
return (NodeList)result;
} catch (XPathExpressionException xpee) {
throw new Error ("An xpath expression exception: " + xpee);
}
}
重度:>> java.lang.Error的:XPath表達式例外:javax.xml.xpath.XPathExpressionException
*「我得到一個異常」*這是'TooManyKittensException'嗎? Desex湯姆貓。還有別的嗎?也許你可能會與我們分享它(即複製/粘貼)。 – 2012-01-30 05:50:43
你在Java代碼中將名稱空間的前綴'atom'關聯(註冊了名稱空間)到名稱空間'「http://www.w3.org/2005/Atom」'嗎?你可以這樣做(推薦)或者使用類似於:'/ */* [local-name()='entry'] [* [local-name()='id'] ='http:// X/feed2/104]'' – 2012-01-30 05:53:11