【发布时间】:2021-09-10 14:12:42
【问题描述】:
这是一个缩短版本,出于 xml 即时接收的安全原因而更改了数据,我正在尝试检查字段“workemail”是否为空,如果不是,我将创建一个:
我已经尝试使用它的绝对路径和相对路径,并使用 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>
【问题讨论】:
-
查看这个答案:stackoverflow.com/a/6392700/3710053 了解如何在 Java 中使用 XPath 中的命名空间