简体   繁体   English

Java XML解析器使用xpath澄清以获取标签值

[英]Java XML parser using xpath clarification to get the tag value

i'm new to xml parser using xpath, please help me to extract the tag value for the below xml 我是使用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>

below is my java code to get the OrganizationFullName 下面是我的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);

while executing getting empty result, Please assist 在执行获取空结果时,请协助

The issue is with the 'sync:' Namespace. 问题出在“同步:”命名空间上。 To handle this, use this XPath (ignoring the namespace completely): 要处理此问题,请使用以下XPath(完全忽略名称空间):

//*[name()='sync:OrganizationFullName']

instead. 代替。 Then the name-variable should be set: 然后应设置名称变量:

"GLOBAL PATENTS/RANDY W. TUNG, ESQ"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM