我是使用xpath的XML解析器的新手,请帮助我提取以下xml的标记值
<?xml version="1.0" encoding="UTF-8"?>
<sync:ApplicationData xsi:schemaLocation="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sync="">
<sync:FileHeader>
<sync:FileCreationTimeStamp>2018-12-14T04:15:54.54</sync:FileCreationTimeStamp>
</sync:FileHeader>
<sync:CorrespondenceInfo>
<sync:OrganizationName>
<sync:OrganizationFullName>GLOBAL PATENTS/RANDY W. TUNG, ESQ </sync:OrganizationFullName>
</sync:OrganizationName>
</sync:CorrespondenceInfo>
下面是我的Java代码,用于获取OrganizationFullName
String filePath = "D:\\cut\\2108.xml";
FileInputStream file = new FileInputStream(new File(filePath));
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document xmlDocument = builder.parse(file);
XPath xPath = XPathFactory.newInstance().newXPath();
xmlDocument.getDocumentElement().normalize();
String name;
name = xPath.compile("//sync:OrganizationFullName").evaluate(xmlDocument);
System.out.println(name);
在执行获取空结果时,请协助
答案 0 :(得分:0)
问题出在'sync:'命名空间。要处理此问题,请使用以下XPath(完全忽略名称空间):
//*[name()='sync:OrganizationFullName']
相反。然后应设置名称变量:
"GLOBAL PATENTS/RANDY W. TUNG, ESQ"