Java 如何从SOAP头中获取头元素名称和值?

Java 如何从SOAP头中获取头元素名称和值?,java,soap,soapheader,Java,Soap,Soapheader,如何从soap请求头中获取用户ID和密码标记名和值 我的请求xml <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://WS.com/"> <soapenv:Header> <userID>34</userID> <password>test</password> </soapenv:H

如何从soap请求头中获取用户ID和密码标记名和值

我的请求xml

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://WS.com/">
<soapenv:Header>
<userID>34</userID>
<password>test</password>
</soapenv:Header>
<soapenv:Body>
</soapenv:Body>
</soapenv:Envelope>
我想获取用户名和密码来验证请求

请帮忙

谢谢

尝试使用以下代码:

// raw SOAP input as String
String input = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ws=\"http://WS.com/\">"
             + "<soapenv:Header>"
             + "<userID>34</userID>"
             + "<password>test</password>"
             + "</soapenv:Header>"
             + "<soapenv:Body>"
             + "</soapenv:Body>"
             + "</soapenv:Envelope>";

// Use MessageFactory with raw input as byte array
InputStream is = new ByteArrayInputStream(input.getBytes());
SOAPMessage message = MessageFactory.newInstance().createMessage(null, is);
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPHeader header = envelope.getHeader();

// obtain all Nodes tagged 'userID' or 'password'
NodeList userIdNode = header.getElementsByTagNameNS("*", "userID");
NodeList passwordNode = header.getElementsByTagNameNS("*", "password");

// extract the username and password
String userId = userIdNode.item(0).getChildNodes().item(0).getNodeValue();
String password = passwordNode.item(0).getChildNodes().item(0).getNodeValue();

System.out.println("userID: " + userId);
System.out.println("password: " + password);

我在字符串userId=userIdNode.item(0.getChildNodes().item(0.getNodeValue)的行中得到“java.lang.NullPointerException”;我仍然在字符串userId=userIdNode.item(0.getNodeValue()行中得到“java.lang.NullPointerException”;NodeList使用的java导入是什么?我仍然在字符串userId=userIdNode.item(0.getNodeValue()行中得到“java.lang.NullPointerException”;导入org.w3c.dom.NodeListSystem.out.println(userIdNode.getLength())后,打印0(零)。我只使用上面的代码获取标题,如果我错了,请帮助。请查看下面给出的答案。代码已经过测试,现在正在运行。
// raw SOAP input as String
String input = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ws=\"http://WS.com/\">"
             + "<soapenv:Header>"
             + "<userID>34</userID>"
             + "<password>test</password>"
             + "</soapenv:Header>"
             + "<soapenv:Body>"
             + "</soapenv:Body>"
             + "</soapenv:Envelope>";

// Use MessageFactory with raw input as byte array
InputStream is = new ByteArrayInputStream(input.getBytes());
SOAPMessage message = MessageFactory.newInstance().createMessage(null, is);
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPHeader header = envelope.getHeader();

// obtain all Nodes tagged 'userID' or 'password'
NodeList userIdNode = header.getElementsByTagNameNS("*", "userID");
NodeList passwordNode = header.getElementsByTagNameNS("*", "password");

// extract the username and password
String userId = userIdNode.item(0).getChildNodes().item(0).getNodeValue();
String password = passwordNode.item(0).getChildNodes().item(0).getNodeValue();

System.out.println("userID: " + userId);
System.out.println("password: " + password);
userID: 34
password: test