gpt4 book ai didi

xpath - Java Xpath 命名空间解析

转载 作者:行者123 更新时间:2023-12-03 16:54:08 26 4
gpt4 key购买 nike

虽然我看到了类似的帖子,但我似乎无法解决这个问题(所以我是手动解析 xml 而不是使用 xpath 表达式)......这是我拥有的 XML......

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:tnew="http://PR_Library/TNeWebService">
<soapenv:Header/>
<soapenv:Body>
<tnew:ApercuenRequest>
<tnew:OpcionCarga>0</tnew:OpcionCarga>
<tnew:Sucursal>1</tnew:Sucursal>
<tnew:Vendedor>8000</tnew:Vendedor>
<tnew:CanalConsulta>1</tnew:CanalConsulta>
<tnew:UsuarioCarga>8000</tnew:UsuarioCarga>
<tnew:Comercio>0</tnew:Comercio>
<tnew:FechaEvaluacion>20140109</tnew:FechaEvaluacion>
<tnew:ModeloDeEvaluacion></tnew:ModeloDeEvaluacion>
<tnew:IdEvaluacion>3694852</tnew:IdEvaluacion>
<tnew:Dudosa>0</tnew:Dudosa>
<tnew:Exceptuado>1</tnew:Exceptuado>
<tnew:TipoDocumento>1</tnew:TipoDocumento>
<tnew:NumeroDocumento>5881040</tnew:NumeroDocumento>
<tnew:Sexo>F</tnew:Sexo>
<tnew:Rol>5</tnew:Rol>
<tnew:RelacionLaboral>5</tnew:RelacionLaboral>
<tnew:TipoEmpleado>0</tnew:TipoEmpleado>
<tnew:FechaIngresoEmpleo></tnew:FechaIngresoEmpleo>
<tnew:AntiguedadLaboral>0</tnew:AntiguedadLaboral>
<tnew:IngresoMensualAnalista>0</tnew:IngresoMensualAnalista>
<tnew:IngresoMensual>6000</tnew:IngresoMensual>
<tnew:EstadoCivil>4</tnew:EstadoCivil>
<tnew:FechaDeNacimiento>19540423</tnew:FechaDeNacimiento>
<tnew:SituacionVivienda>3</tnew:SituacionVivienda>
<tnew:DomicilioProvincia></tnew:DomicilioProvincia>
<tnew:DomicilioLocalidad></tnew:DomicilioLocalidad>
<tnew:NivelEstudios></tnew:NivelEstudios>
<tnew:RelacionConTN>1</tnew:RelacionConTN>
<tnew:FechaAltaAbogado></tnew:FechaAltaAbogado>
<tnew:MarcaAbogado>N</tnew:MarcaAbogado>
<tnew:Cdi></tnew:Cdi>
<tnew:Campana></tnew:Campana>
</tnew:ApercuenRequest>
</soapenv:Body>
</soapenv:Envelope>

而下面的Java代码...
private XPathFactory xpathFactory = null;  
private XPath xpath = null;
private DocumentBuilderFactory factory = null;
private DocumentBuilder builder= null;
private XPathExpression expr;
private Node nodo = null;
private Node nodoAux = null;
private NodeList result = null;
private NodeList nodeList = null;

...

try {
xpathFactory = XPathFactory.newInstance();
xpath = xpathFactory.newXPath();
factory = DocumentBuilderFactory.newInstance();
builder= factory.newDocumentBuilder();

NamespaceContext context = new NameSpaceNevada();
xpath.setNamespaceContext(context);
}
catch (Exception e) {

}

...

evaluarExpresion(documentoXML,"/soapenv:Envelope/soapenv:Body/tnew:ApercuenRequest/tnew:OpcionCarga")

private String evaluarExpresion(Document documentoXML, String expresion) throws Exception{
try {
expr = xpath.compile(expresion);
result = (NodeList)expr.evaluate(documentoXML, XPathConstants.NODESET);
System.out.println((result.item(0)!=null?result.item(0).getNodeName():"") + " ->>>> " + (result.item(0).getFirstChild()!=null?result.item(0).getFirstChild().getNodeValue():""));
return result.item(0).getFirstChild()!=null?result.item(0).getFirstChild().getNodeValue():null;
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}

而且我似乎无法获得您在 XML 中看到的“0”值......我已经尝试了很多东西......我会看......问题是,我可以手动解析它但这个笑话是通过 xpath 表达式来实现的,所以我可以保存代码......

希望我让我明白...
getNamespaceURI方法来自 NameSpaceNevada对象如下:
public String getNamespaceURI(String prefix) {
if (prefix == null) {
throw new IllegalArgumentException("No prefix provided!");
} else if (prefix.equals(XMLConstants.DEFAULT_NS_PREFIX)) {
return "http://univNaSpResolver/book";
} else if (prefix.equals("soapenv")) {
return "http://schemas.xmlsoap.org/soap/envelope/";
} else if (prefix.equals("tnew")) {
return "http://PR_Library/TNeWebService";
} else {
return XMLConstants.NULL_NS_URI;
}
}

最佳答案

与其将表达式作为节点集进行评估,然后再执行复杂的 DOM 操作来提取文本值,您还可以首先将表达式作为字符串进行评估。

expr.evaluate(documentoXML);

将评估表达式并使用 XPath 规则将结果转换为字符串,在这种情况下,它将为您提供第一个(实际上是唯一的) tnew:OpcionCarga 的文本内容。元素。

但它不工作的真正原因是Java的 DocumentBuilderFactory默认情况下是非命名空间感知的(在许多人看来这是一个非常糟糕的设计决策,但现在无法在不破坏向后兼容性的情况下进行纠正)-您必须这样做
factory.setNamespaceAware(true);

在您调用之前 .newDocumentBuilder()为了创建命名空间感知解析器。

关于xpath - Java Xpath 命名空间解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21186888/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com