【发布时间】:2018-10-20 04:02:32
【问题描述】:
我有一个如下所示的 XML,我正在使用 XPath 读取 XML。由于我知道数据的确切位置,因此我将路径动态传递给 Expression 对象并获取值。
<MESSAGE xmlns:xlink="http://www.w3.org/1999/xlink">
<ABOUT_VERSIONS>
<ABOUT_VERSION SequenceNumber="1" xlink:label="ASSET_1" >
<CreatedDatetime>2015-08-24T09:30:47Z</CreatedDatetime>
<DataVersionName>Purchase Example</DataVersionName>
</ABOUT_VERSION>
</ABOUT_VERSIONS>
<DEALS>
<DEAL>
<ACCOUNT>
<Name>Test</Name>
</ACCOUNT>
</DEAL>
</DEALS>
</MESSAGE>
我正在使用下面的代码来读取 XML。
XPathFactory xpf = XPathFactory.newInstance();
XPath xPath = xpf.newXPath();
XPathExpression pathExpression =
xPath.compile("MESSAGE/ABOUT_VERSIONS/ABOUT_VERSION");
InputSource inputSource = new InputSource("C:/Sample.xml");
NodeList Nodes = (NodeList)
xPath.evaluate("MESSAGE/ABOUT_VERSIONS/ABOUT_VERSION", inputSource,
XPathConstants.NODESET);
有没有这种方法可以用 Java 编写 XML?稍后我需要用额外的标签和额外的数据编写相同的 XML。
【问题讨论】: