Analyser avec des espaces de noms XML en Java à l'aide de xpath
Je suis en train d'analyser une requête SOAP en java, mais le code n'est pas de retourner tous les nœuds
voici le code quelqu'un peut-il trouver une erreur
String xml="<soapenv:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://services.web.post.list.com\"><soapenv:Header><authInfo xsi:type=\"soap:authentication\" xmlns:soap=\"http://list.com/services/SoapRequestProcessor\"><!--You may enter the following 2 items in any order--><username xsi:type=\"xsd:string\">[email protected]</username><password xsi:type=\"xsd:string\">PfasdfRem91</password></authInfo></soapenv:Header></soapenv:Envelope>";
System.out.println(xml);
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(xml)));
XPath xpath = XPathFactory.newInstance().newXPath();
//XPath Query for showing all nodes value
try
{
XPathExpression expr = xpath.compile("/soapenv:Envelope/soapenv:Header/authInfo/password");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
System.out.println("Got " + nodes.getLength() + " nodes");
// System.out.println(nodes.item(0).getNodeValue());
}
catch(Exception E)
{
System.out.println(E);
}
- Essayez ces : stackoverflow.com/questions/5673708/... stackoverflow.com/questions/5519766/...
- merci je vais essayer ces
Vous devez vous connecter pour publier un commentaire.
Vous devez définir un
NamespaceContext
sur leXPath
:Démo
Sortie
Ajouter un peu plus à la dernière réponse..
Pour obtenir le nœud particulier de la valeur, vous pouvez utiliser la sous--
[
System.out.println(nodes.item(0).getTextContent());
]