简体   繁体   中英

unable to retrieve value from xml element using xPath

Hi to everyone I'm trying to use xPath to retrieve the value of some xml element but all the NodeList doesn't contain the nodes' values. Could anyone help me?

This is the xml I want to retrieve:

<?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>

And this is the code:

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();
        }

Resolved in this way, in any case thank you:

            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();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM