栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用xpath在Java中使用名称空间解析XML

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用xpath在Java中使用名称空间解析XML

您需要设置一个

NamespaceContext
XPath

演示版

package forum11644994;import java.io.StringReader;import java.util.Iterator;import javax.xml.namespace.NamespaceContext;import javax.xml.parsers.*;import javax.xml.xpath.*;import org.w3c.dom.document;import org.w3c.dom.NodeList;import org.xml.sax.InputSource;public class Demo {    public static void main(String[] args) throws Exception {        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">dfasf@google.com</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.setNamespaceContext(new NamespaceContext() { @Override public Iterator getPrefixes(String arg0) {     return null; } @Override public String getPrefix(String arg0) {     return null; } @Override public String getNamespaceURI(String arg0) {     if("soapenv".equals(arg0)) {         return "http://schemas.xmlsoap.org/soap/envelope/";     }     return null; }        });        // 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);        }    }}

输出量

<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">dfasf@google.com</username><password xsi:type="xsd:string">PfasdfRem91</password></authInfo></soapenv:Header></soapenv:Envelope>Got 1 nodes


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/431150.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号