【发布时间】:2012-02-06 16:53:58
【问题描述】:
我也有一个 XML
<?xml version="1.0" encoding="UTF-8"?>
<QDTM_IN300301QD ITSVersion="XML_1.0" xmlns="urn:hl7-org:v3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:hl7-org:v3 QDTM_IN300401QD.xsd ">
<controlActEvent classCode="CACT" moodCode="EVN">
<code code="QDTM_TE300401QD">
</code>
<statusCode code="Active" />
<subject contextConductionInd="true" contextControlCode="ON"
typeCode="SUBJ">
<registrationEvent classCode="REG" moodCode="EVN">
<statusCode code="token" />
<subject contextControlCode="AN" typeCode="SBJ">
<testCodeIdentifier classCode="ROL">
<playingTestCodeDetails classCode="ENT"
determinerCode="INSTANCE">
<code code="6399Z" codeSystemName="QTIM" codeSystemVersion="Updated">
<originalText><![CDATA[CBC (includes Differential and Platelets)]]></originalText>
<translation codeSystemName="DOSCATALOGNAMEHTMLENABLED">
<originalText><![CDATA[CBC (includes Differential and Platelets)]]></originalText>
</translation>
</code>
</playingTestCodeDetails>
</testCodeIdentifier>
</subject>
</registrationEvent>
</subject>
</controlActEvent>
</QDTM_IN300301QD>
JAVA 代码:
package com.parse;
import java.io.IOException;
import java.util.Iterator;
import javax.xml.namespace.NamespaceContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
public class ParseXPath {
public String parseXML(String fileName) {
fileName = "D://projects//Draft.xml";
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder builder;
Document doc;
try {
builder = domFactory.newDocumentBuilder();
doc = builder.parse(fileName);
XPath xpath = XPathFactory.newInstance().newXPath();
xpath.setNamespaceContext(new NamespaceContext(){public String getNamespaceURI(String prefix) {
return "urn:hl7-org:v3";
}
public String getPrefix(String namespaceURI) {
return null; // we are not using this.
}
public Iterator getPrefixes(String namespaceURI) {
return null; // we are not using this.
}
});
String expr="//QDTM_IN300401QD/controlActEvent/subject/registrationEvent/subject/testCodeIdentifier/playingTestCodeDetails/code/translation[@codeSystemName='DOSCATALOGNAMEHTMLENABLED']/originalText/text()";
String result = xpath.evaluate(expr, doc);
System.out.println("Result --> "+result);
return result;
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XPathExpressionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return fileName;
public static void main(String[] args)
throws ParserConfigurationException, SAXException,
IOException, XPathExpressionException {
ParseBUXpath p = new ParseBUXpath();
p.parseRelatedTestXML("test");
}
}
我在 XML 中的命名空间中遇到了这个问题。当 xml 具有“xmlns="urn:hl7-org:v3" 时,xpath 查询不会让我得到数据。为了抑制我已经在 java 中编写了代码并从 XML 中删除了该行。
我需要在不从 XML 中删除命名空间部分的情况下解析 XML 并获取数据。这是与 xsd 相关的问题还是没有提到 xsd?
【问题讨论】:
-
如果我错了“您想在解析 XML 文档时获取名称空间”,请纠正我对吗?
-
我需要使用 XPath 表达式从 XML 获取数据,并且 XML 中的命名空间阻止它获取数据....为什么要这样做.. 为了抑制我已经删除了行XML 和 Java 代码中添加的代码。
-
我刚刚进行了以下行更改,它对我有用..: domFactory.setNamespaceAware(false);// 将其设为真假 如果我遇到错误的方面,请纠正我.. .
-
@GOK - 这通常是错误的方向。命名空间很重要。看我的回答。
标签: java xml parsing xpath namespaces