Java 无法使用xPath从xml元素检索值
大家好,我正在尝试使用xPath检索某个xml元素的值,但所有节点列表都不包含节点的值。有人能帮我吗 这是我要检索的xml:Java 无法使用xPath从xml元素检索值,java,xml,xpath,Java,Xml,Xpath,大家好,我正在尝试使用xPath检索某个xml元素的值,但所有节点列表都不包含节点的值。有人能帮我吗 这是我要检索的xml: <?xml version="1.0" ?> <Documenti UID="69431" UTC="20-04-2018 13:35:21 UTC(+0000)" NomeDA="Documento-Amministrativo-Informatico-PA" HASH_PDV="F65ECB12CD2A0344BEAAB7250B4C228490
<?xml version="1.0" ?>
<Documenti UID="69431" UTC="20-04-2018 13:35:21 UTC(+0000)"
NomeDA="Documento-Amministrativo-Informatico-PA"
HASH_PDV="F65ECB12CD2A0344BEAAB7250B4C228490C3DCF0FD845775B3FFF7055D780BE6"
HASH_PDV_TYPE="SHA-256" schemaVersion="3.0">
<errori>
<errore>
Riga 1: METADATI_DUPLICATI: I Metadati "UID_Documento" sono
definiti come univoci, ma i seguenti valori dichiarati sul file di bb
indice sono gia' presenti a sistema: [UID_Documento=335]
</errore>
<errore>
Riga 2: METADATI_DUPLICATI: I Metadati "UID_Documento" sono
definiti come univoci, ma i seguenti valori dichiarati sul
file di indice sono gia' presenti a sistema:
[UID_Documento=334]
</errore>
<errore>
Errore durante la validazione documenti per il processo con id 69431
Dati non validati: Riga 1: METADATI_DUPLICATI: I Metadati
"UID_Documento" sono definiti come univoci, ma i seguenti valori
dichiarati sul file di indice sono gia' presenti a sistema:
[UID_Documento=335]
</errore>
<errore>
Riga 2: METADATI_DUPLICATI: I Metadati "UID_Documento" sono
definiti come univoci, ma i seguenti valori dichiarati sul file
di indice sono gia' presenti a sistema: [UID_Documento=334]
</errore>
里加1:METADATI_replici:I METADATI“UID_Documento”sono
明确地说,这是一个完整的bb文件
指示是否存在系统:[UID\u Documento=335]
里加2:METADATI_replici:I METADATI“UID_Documento”sono
明确地说,我是南瓦洛里地区的一员
指示系统存在的文件:
[UID_Documento=334]
根据ILProcesso con id 69431的有效期文件错误
非验证数据:Riga 1:METADATI_replici:I METADATI
“UID_Documento”是一个明确的定义,它是一个独立的概念
指示系统存在的两个sul文件:
[UID_Documento=335]
里加2:METADATI_replici:I METADATI“UID_Documento”sono
明确地说,这是一份完整的文件
系统存在的迹象:[UID\u Documento=334]
这是代码:
byte[] bytesP7m =out.toByteArray();
File tmpf=new File(Folium.getProperty(session, Folium.GLOBAL_LOCAL_PATH)+File.separator+"error"+File.separator+"error.xml");
FileUtils.writeByteArrayToFile(tmpf, bytesP7m);
//BufferedReader br=new BufferedReader(new FileReader(tmpf.getName()));
FileInputStream fileIS = new FileInputStream(tmpf);
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document xmlDocument = builder.parse(fileIS);
XPath xPath = XPathFactory.newInstance().newXPath();
String expression = "//errori";
NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
for (int i=0; i<nodeList.getLength(); i++) {
//String node=nodeList.item(i).getNodeValue();
returnError=returnError+"\r\n"+nodeList.item(i).getNodeValue();
}
byte[]bytesP7m=out.toByteArray();
File tmpf=新文件(Folium.getProperty(session,Folium.GLOBAL\u LOCAL\u PATH)+File.separator+“error”+File.separator+“error.xml”);
writeByteArrayFile(tmpf,bytesP7m);
//BufferedReader br=新的BufferedReader(新文件读取器(tmpf.getName());
FileInputStream fileIS=新的FileInputStream(tmpf);
DocumentBuilderFactory builderFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder=builderFactory.newDocumentBuilder();
Document xmlDocument=builder.parse(fileIS);
XPath=XPathFactory.newInstance().newXPath();
字符串表达式=“//errori”;
NodeList NodeList=(NodeList)xPath.compile(expression.evaluate(xmlDocument,XPathConstants.NODESET);
对于(int i=0;i以这种方式解决,无论如何,谢谢:
File tmpf=new File(Folium.getProperty(session, Folium.GLOBAL_LOCAL_PATH)+File.separator+"error"+File.separator+"error.xml");
FileUtils.writeByteArrayToFile(tmpf, bytesP7m);
//BufferedReader br=new BufferedReader(new FileReader(tmpf.getName()));
FileInputStream fileIS = new FileInputStream(tmpf);
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document xmlDocument = builder.parse(fileIS);
NodeList nodeList = xmlDocument.getElementsByTagName("errore");
for (int i=0; i<nodeList.getLength(); i++) {
String node=nodeList.item(i).getFirstChild().getNodeValue();
returnError=returnError+"\r\n"+nodeList.item(i).getFirstChild().getNodeValue();
}
File tmpf=新文件(Folium.getProperty(session,Folium.GLOBAL\u LOCAL\u PATH)+File.separator+“error”+File.separator+“error.xml”);
writeByteArrayFile(tmpf,bytesP7m);
//BufferedReader br=新的BufferedReader(新文件读取器(tmpf.getName());
FileInputStream fileIS=新的FileInputStream(tmpf);
DocumentBuilderFactory builderFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder=builderFactory.newDocumentBuilder();
Document xmlDocument=builder.parse(fileIS);
NodeList NodeList=xmlDocument.getElementsByTagName(“error”);
对于(int i=0;i只选择了一个节点:errori。如果您想要它的完整文本,应该使用getTextContent()而不是getNodeValue()。