简体   繁体   English

我如何将 XPath 与 Java 一起用于此 XML?

[英]How can i go use XPath with java for this XML?

This is a shortened version with altered data for security reasons of the xml im receiving, and I'm trying to check if the field 'workemail' is empty and if not I'm going to create one:这是一个缩短的版本,出于安全原因,我收到的 xml 的数据已更改,我正在尝试检查“workemail”字段是否为空,如果不是,我将创建一个:

i have tried using the absolute and relative path for it and using getLength to see if it even gives me anything but I only get:我尝试使用绝对路径和相对路径,并使用 getLength 来查看它是否给了我任何东西,但我只得到:

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>

You need to change from:您需要更改为:

Document doc = builder.parse(input);

To:至:

Document doc = builder.parse(new File("C:\\\\temp\\\\test.xml"));

See following for explanation: java.net.MalformedURLException: no protocol请参阅以下说明: java.net.MalformedURLException: no protocol

Couple of things:几件事:

  1. I did not find any use for following:我没有发现以下任何用途:

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

  2. You would probably not find email because your xml has namespace definition.您可能找不到电子邮件,因为您的 xml 具有名称空间定义。 If you encounter that, see this:如果您遇到这种情况,请参阅:

how to ignore namespaces with XPath 如何使用 XPath 忽略命名空间

  1. Your xml is malformed.您的 xml 格式错误。 I have given the fixed one below:我在下面给出了固定的:

     <?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>TT</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>

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

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