我花了一天的时间尝试从以下文档中提取一个XML节点,并且无法掌握XML名称空间的细微差别以使其工作。XPath,XML命名空间和Java
XML文件是大的总张贴所以这里是我所关心的部分:
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<XFDL xmlns="http://www.PureEdge.com/XFDL/6.5" xmlns:custom="http://www.PureEdge.com/XFDL/Custom" xmlns:designer="http://www.PureEdge.com/Designer/6.1" xmlns:pecs="http://www.PureEdge.com/PECustomerService" xmlns:xfdl="http://www.PureEdge.com/XFDL/6.5">
<globalpage sid="global">
<global sid="global">
<xmlmodel xmlns:xforms="http://www.w3.org/2003/xforms">
<instances>
<xforms:instance id="metadata">
<form_metadata>
<metadataver version="1.0"/>
<metadataverdate>
<date day="05" month="Jul" year="2005"/>
</metadataverdate>
<title>
<documentnbr number="2062" prefix.army="DA" scope="army" suffix=""/>
<longtitle>HAND RECEIPT/ANNEX NUMBER </longtitle>
</title>
文档过程继续,以及形成一路下滑。我试图从“documentnbr”标签(从底部三个)中提取“数字”属性。
,我使用做到这一点的代码如下所示:
/***
* Locates the Document Number information in the file and returns the form number.
* @return File's self-declared number.
* @throws InvalidFormException Thrown when XPath cannot find the "documentnbr" element in the file.
*/
public String getFormNumber() throws InvalidFormException
{
try{
XPath xPath = XPathFactory.newInstance().newXPath();
xPath.setNamespaceContext(new XFDLNamespaceContext());
Node result = (Node)xPath.evaluate(QUERY_FORM_NUMBER, doc, XPathConstants.NODE);
if(result != null) {
return result.getNodeValue();
} else {
throw new InvalidFormException("Unable to identify form.");
}
} catch (XPathExpressionException err) {
throw new InvalidFormException("Unable to find form number in file.");
}
}
哪里QUERY_FORM_NUMBER是我的XPath表达式,并XFDLNamespaceContext实现NamespaceContext,看起来像这样:
public class XFDLNamespaceContext implements NamespaceContext {
@Override
public String getNamespaceURI(String prefix) {
if (prefix == null) throw new NullPointerException("Invalid Namespace Prefix");
else if (prefix.equals(XMLConstants.DEFAULT_NS_PREFIX))
return "http://www.PureEdge.com/XFDL/6.5";
else if ("custom".equals(prefix))
return "http://www.PureEdge.com/XFDL/Custom";
else if ("designer".equals(prefix))
return "http://www.PureEdge.com/Designer/6.1";
else if ("pecs".equals(prefix))
return "http://www.PureEdge.com/PECustomerService";
else if ("xfdl".equals(prefix))
return "http://www.PureEdge.com/XFDL/6.5";
else if ("xforms".equals(prefix))
return "http://www.w3.org/2003/xforms";
else
return XMLConstants.NULL_NS_URI;
}
@Override
public String getPrefix(String arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public Iterator getPrefixes(String arg0) {
// TODO Auto-generated method stub
return null;
}
}
我已经尝试了许多不同的XPath查询,但我一直觉得这应该工作:
protected static final String QUERY_FORM_NUMBER =
"/globalpage/global/xmlmodel/xforms:instances/instance" +
"/form_metadata/title/documentnbr[number]";
不幸的是,它不起作用,我不断得到一个空回报。
我已经完成了相当数量的阅读here,here和here,但没有任何证据足以说明我的工作。
当我明白这一点时,我几乎肯定我要面子,但我真的很机智地结束了我失踪的事情。
感谢您阅读所有这些内容,并提前感谢您的帮助。
-Andy
完美解决方案,我还是不太明白的命名空间,但在现在最少的代码工作。 非常感谢。 – MrWizard54 2011-03-29 11:16:36
@Jason:“由于某种原因,我最终没有在表达式中使用任何名称空间。”在标准的Java实现中,默认情况下,DocumentBuilderFactory会生成namespace-_unaware_分析器。在生成DocumentBuilder之前添加'dbfac.SetNamespaceAware(true)'可能会改变结果。 – 2013-03-08 15:18:09