0

I have written a Groovy that have as an input an xml file and i want to extract the value of two tags that are part of the xml file.I have converted XML to Json and from JSON to Map. This Java code works for me:

    import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import org.apache.commons.io.IOUtils;
import org.json.JSONObject;
import org.json.XML;
import com.bfi.digi.chk.CheckRemittance;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
CheckRemittance remise =data;
    
         
InputStream inputStream = new FileInputStream(new File(
            "C:/xml/full_rejection_notification.xml"));
String xml = IOUtils.toString(inputStream);
 
    JSONObject jObject = XML.toJSONObject(xml);
    ObjectMapper mapper = new ObjectMapper();
    mapper.enable(SerializationFeature.INDENT_OUTPUT);
    Object json = mapper.readValue(jObject.toString(), Object.class);
    String output = mapper.writeValueAsString(json);
 
Map<String,  List<String>> response = new ObjectMapper().readValue(output, HashMap.class); 
   String state= response.get("Document").get("FIToFIPmtStsRpt").get("TxInfAndSts").get("TxSts").toString();
String errorCode=response.get("Document").get("FIToFIPmtStsRpt").get("TxInfAndSts").get("StsRsnInf").get("Rsn").get("Cd").toString();
return state.concat(" ").concat(errorCode);

The problem is that i'm getting an xml file containing repetitive block and as Map don't allow duplicate key I have to find another solution. I'm looking for your propositions.

  • Can you not simply parse the XML file and then call [getElementsByTagName](https://docs.oracle.com/en/java/javase/11/docs/api/java.xml/org/w3c/dom/Element.html#getElementsByTagName(java.lang.String))? Refer to https://stackoverflow.com/questions/4076910/how-to-retrieve-element-value-of-xml-using-java – Abra Apr 09 '21 at 13:04
  • Can you please show the xml? Or an example which describe your case? – KunLun Apr 09 '21 at 13:17

0 Answers0