0

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:

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:

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>
Nimantha
  • 6,405
  • 6
  • 28
  • 69

1 Answers1

0

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

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. If you encounter that, see this:

how to ignore namespaces with XPath

  1. Your xml is malformed. 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>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>
    
Nimantha
  • 6,405
  • 6
  • 28
  • 69
  • thanks for the reply! but i get the xml from an api call and its not a file on my computer so the "input" im using there comes from a function i used to get the xml. – saruronin Jun 28 '21 at 10:10
  • @saruronin Okay. Then you can use approach in https://stackoverflow.com/questions/562160/in-java-how-do-i-parse-xml-as-a-string-instead-of-a-file –  Jun 28 '21 at 10:17