当前位置: 首页 > 知识库问答 >
问题:

如何将XPath与java一起用于此XML

沈飞翼
2023-03-14
java.net.MalformedURLException: no protocol



public void checkEmployeeEmail(String input) throws IOException, ParserConfigurationException, SAXException, XPathExpressionException {
        DocumentBuilderFactory factory =
                DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();

        StringBuilder xmlStringBuilder = new StringBuilder();
        ByteArrayInputStream inputStream =  new ByteArrayInputStream(
                xmlStringBuilder.toString().getBytes("UTF-8"));
        Document doc = builder.parse(input);
        XPath xPath =  XPathFactory.newInstance().newXPath();
        String expression = "//*/PersonalInfo_GetAll_AllEmployeesByCompanyResult/PersonalInfoItem/EmailWork";
        NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(doc, XPathConstants.NODESET);
        System.out.println(nodeList.getLength());

    }
}




<?xml version="1.0" encoding="utf-8"?>

<soap:Envelope
    xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">

    <soap:Body>
        <PersonalInfo_GetAll_AllEmployeesByCompanyResponse
            xmlns="https://api.nmbrs.nl/soap/v3/EmployeeService">
            <PersonalInfo_GetAll_AllEmployeesByCompanyResult>
                <PersonalInfoItem>
                    <EmployeeId>1</EmployeeId>
                    <EmployeePersonalInfos>
                        <PersonalInfo_V2>
                            <Id>1</Id>
                            <DisplayName>person 1</DisplayName>
                            <EmployeeNumber>7</EmployeeNumber>
                            <FirstName>firstname1</FirstName>
                            <Initials>T.T</Initials>
                            <LastName>firstname</LastName>
                            <Nickname>lastname</Nickname>
                            <Gender>female</Gender>
                            <NationalityCode>1</NationalityCode>
                            <PlaceOfBirth>amsterdam</PlaceOfBirth>
                            <CountryOfBirthISOCode>NL</CountryOfBirthISOCode>
                            <IdentificationNumber>NV2H6DD52</IdentificationNumber>
                            <IdentificationType>10</IdentificationType>
                            <TelephoneMobilePrivate>0000000</TelephoneMobilePrivate>
                            <TelephoneMobileWork>000000</TelephoneMobileWork>
                            <EmailPrivate>person@gmail.com</EmailPrivate>
                            <EmailWork>tblabla@workemail.com</EmailWork>
                            <BurgerlijkeStaat>2</BurgerlijkeStaat>
                            <Naamstelling>1</Naamstelling>
                            <Birthday>1994-04-14T00:00:00</Birthday>
                            <CreationDate>2020-07-30T08:54:41.04</CreationDate>
                            <StartPeriod>7</StartPeriod>
                            <StartYear>2020</StartYear>
                        </PersonalInfo_V2>
                        <PersonalInfo_V2>
                            <Id>98351</Id>
                            <DisplayName>person2</DisplayName>
                            <EmployeeNumber>7</EmployeeNumber>
                            <LastName>lastname2</LastName>
                            <Nickname>firstname2</Nickname>
                            <Gender>undefined</Gender>
                            <NationalityCode>0</NationalityCode>
                            <IdentificationType>0</IdentificationType>
                            <EmailWork>person2@workemail.com</EmailWork>
                            <BurgerlijkeStaat>0</BurgerlijkeStaat>
                            <Naamstelling>0</Naamstelling>
                            <Birthday>0001-01-01T00:00:00</Birthday>
                            <CreationDate>2020-06-17T12:52:21.273</CreationDate>
                            <StartPeriod>1</StartPeriod>
                            <StartYear>1</StartYear>
                        </PersonalInfo_V2>
                    </EmployeePersonalInfos>
                </PersonalInfoItem>
                 </EmployeePersonalInfos>
                </PersonalInfoItem>
            </PersonalInfo_GetAll_AllEmployeesByCompanyResult>
        </PersonalInfo_GetAll_AllEmployeesByCompanyResponse>
    </soap:Body>
</soap:Envelope>

抱歉,如果这个帖子不好,这是我在这里的第一个问题:)

共有1个答案

施子民
2023-03-14

您需要从:

文档doc=builder.parse(input);

致:

ByteArrayInputStream inputStream=new ByteArrayInputStream(xmlStringBuilder.toString().getBytes(“UTF-8”);

您可能找不到email,因为xml具有命名空间定义。如果你遇到这种情况,请看以下内容:

如何使用XPath忽略命名空间

   <?xml version="1.0" encoding="utf-8"?>
   <soap:Envelope
   xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:xsd="http://www.w3.org/2001/XMLSchema">

   <soap:Body>
       <PersonalInfo_GetAll_AllEmployeesByCompanyResponse
           xmlns="https://api.nmbrs.nl/soap/v3/EmployeeService">
           <PersonalInfo_GetAll_AllEmployeesByCompanyResult>
               <PersonalInfoItem>
                   <EmployeeId>1</EmployeeId>
                   <EmployeePersonalInfos>
                       <PersonalInfo_V2>
                           <Id>1</Id>
                           <DisplayName>person 1</DisplayName>
                           <EmployeeNumber>7</EmployeeNumber>
                           <FirstName>firstname1</FirstName>
                           <Initials>T.T</Initials>
                           <LastName>firstname</LastName>
                           <Nickname>lastname</Nickname>
                           <Gender>female</Gender>
                           <NationalityCode>1</NationalityCode>
                           <PlaceOfBirth>amsterdam</PlaceOfBirth>
                           <CountryOfBirthISOCode>NL</CountryOfBirthISOCode>
                           <IdentificationNumber>NV2H6DD52</IdentificationNumber>
                           <IdentificationType>10</IdentificationType>
                           <TelephoneMobilePrivate>0000000</TelephoneMobilePrivate>
                           <TelephoneMobileWork>000000</TelephoneMobileWork>
                           <EmailPrivate>person@gmail.com</EmailPrivate>
                           <EmailWork>tblabla@workemail.com</EmailWork>
                           <BurgerlijkeStaat>2</BurgerlijkeStaat>
                           <Naamstelling>1</Naamstelling>
                           <Birthday>1994-04-14T00:00:00</Birthday>
                           <CreationDate>2020-07-30T08:54:41.04</CreationDate>
                           <StartPeriod>7</StartPeriod>
                           <StartYear>2020</StartYear>
                       </PersonalInfo_V2>
                       <PersonalInfo_V2>
                           <Id>98351</Id>
                           <DisplayName>person2</DisplayName>
                           <EmployeeNumber>7</EmployeeNumber>
                           <LastName>lastname2</LastName>
                           <Nickname>firstname2</Nickname>
                           <Gender>undefined</Gender>
                           <NationalityCode>0</NationalityCode>
                           <IdentificationType>0</IdentificationType>
                           <EmailWork>person2@workemail.com</EmailWork>
                           <BurgerlijkeStaat>0</BurgerlijkeStaat>
                           <Naamstelling>0</Naamstelling>
                           <Birthday>0001-01-01T00:00:00</Birthday>
                           <CreationDate>2020-06-17T12:52:21.273</CreationDate>
                           <StartPeriod>1</StartPeriod>
                           <StartYear>1</StartYear>
                       </PersonalInfo_V2>                        
                   </EmployeePersonalInfos>
               </PersonalInfoItem>
           </PersonalInfo_GetAll_AllEmployeesByCompanyResult>
       </PersonalInfo_GetAll_AllEmployeesByCompanyResponse>
   </soap:Body>
 </soap:Envelope>
 类似资料:
  • 我试图用以下代码单击元素: 它返回一个错误, 如何使用xpath正确点击?

  • 我想ssh到代理后面的机器,然后在我的java程序中进行portforwarding。(为了能够ssh到盒子,我应该先ssh到代理机器)。我通常通过在我的~/.ssh/config文件中添加以下条目来实现: 谢了!

  • 在我的脚本中使用xpath有问题。当我使用Appium检查器使用xpath“//android.widget.textView[@text='My Messages']”定位和元素时,检查器没有任何问题地定位它。但是当我试图在代码中使用xpath时 我收到一条错误消息,上面写着 noSuChelementException:无法使用给定的搜索参数在页面上找到元素。(警告:服务器未提供任何堆栈跟踪信

  • 问题内容: 我正在使用BeautifulSoup抓取网址,并且我有以下代码 现在在上面的代码中,我们可以用来获取标签和与其相关的信息,但是我想使用xpath。是否可以将xpath与BeautifulSoup一起使用?如果可能的话,任何人都可以给我提供示例代码,以使其更有帮助吗? 问题答案: 不,BeautifulSoup本身不支持XPath表达式。 另一种库,LXML, 不 支持的XPath 1.

  • 问题内容: 我正在尝试使用Text Finder插件编写jenkinsfile ,但我不知道它是如何工作的。 这是我的代码: 问题答案: 正如@mghicks已经提到的,并不是每个插件都支持Jenkins管道。在这种情况下,Text Finder插件不支持它。例如,您可以为此编写自己的groovy函数: 例如: 我们正在调用filterLogs函数,并提供参数“ ERROR”(在您的日志中搜索ER

  • 我在Ubuntu 14.04中安装了phpbrew以使用PHP5.4。它工作得很好,但当我尝试使用composer时除外,composer会忽略phpbrew并安装与PHP5.5相关的依赖项。 有没有办法强制composer与phpbrew config兼容?我试图在我的作曲家中添加:php:“5.4”。json,但是它说这个要求在我的php版本中是不可能的。