Java 如何使用Jdom获取特定的标记值
我试图使用Jdom解析器从XML中获取特定值。 下面是我的xml:Java 如何使用Jdom获取特定的标记值,java,xpath,xml-parsing,jdom,Java,Xpath,Xml Parsing,Jdom,我试图使用Jdom解析器从XML中获取特定值。 下面是我的xml: <recordTarget> <patientRole> **<id root="1.20.3.01.5.2" extension="a"/> <id root="1.2.0.5.1.3.2" extension="b"/>** <addr use=""><country></country><s
<recordTarget>
<patientRole>
**<id root="1.20.3.01.5.2" extension="a"/>
<id root="1.2.0.5.1.3.2" extension="b"/>**
<addr use=""><country></country><state></state><city></city><postalCode></postalCode><streetAddressLine></streetAddressLine></addr>
<telecom value="" use=""/>
<telecom value="" use=""/>
<patient>
</patient>
<providerOrganization>
</providerOrganization>
</patientRole>
</recordTarget>
**
**
现在,从上面的xml中,我想在中的'ID'标记(用asterik标记)下获得'extension'属性,它的值为“3.2”,而忽略包含“5.2”的ID标记
我可以得到第一个值,但我需要得到第二个id标签值
下面是我的java代码,它为我提供了ID扩展名的第一个值:
XPathExpression<Attribute> expr = xFactory.compile(xPath, Filters.attribute(), null, defaultNs);
Attribute attribute = expr.evaluateFirst(document);
if (attribute != null) {
return attribute.getValue();
} else {
return "";
}
XPathExpression expr=xFactory.compile(xPath,Filters.attribute(),null,defaultNs);
Attribute Attribute=expr.evaluateFirst(文档);
if(属性!=null){
返回属性.getValue();
}否则{
返回“”;
}
您没有显示实际使用的xPath
是什么,但我认为xPath类似于:
//id[contains(@root, '3.2')]/@extension
我们应该做到这一点
对我来说,我用以下方法运行它:
String xPath = "//id[contains(@root, '3.2')]/@extension";
XPathFactory xFactory = XPathFactory.instance();
XPathExpression<Attribute> expr = xFactory.compile(xPath, Filters.attribute());
Attribute attribute = expr.evaluateFirst(document);
if (attribute != null) {
System.out.println(attribute.getValue());
} else {
System.out.println("foobar");
}
String xPath=“//id[包含(@root,'3.2')]/@extension”;
XPathFactory xFactory=XPathFactory.instance();
XPathExpression。您可以获得一个具体id的扩展名
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.junit.Test;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
public class XmlDomTest {
@Test
public void getSecondIdFromXml() {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder;
Document doc = null;
try {
builder = factory.newDocumentBuilder();
doc = builder.parse(getClass().getClassLoader().getResourceAsStream("your_file"));
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
System.out.println("Extension: " + getExtensionById(doc, xpath, "1.2.0.5.1.3.2"));
} catch (ParserConfigurationException | SAXException | IOException e) {
e.printStackTrace();
}
}
private String getExtensionById(Document doc, XPath xpath, String id) {
String value= null;
try {
XPathExpression expr = xpath.compile("//id[@root='" + id + "']/@extension");
value= (String) expr.evaluate(doc, XPathConstants.STRING);
} catch (XPathExpressionException e) {
e.printStackTrace();
}
return value;
}}
我使用xpath作为//recordTarget/patientRole/id/@扩展名,我从属性文件@Sergio获取xpath-实际上id不是具体的,但它将包含“3.2”,我需要验证并选择该根的扩展名。上面rolfl的答案是通过更改xpath来实现的。