Java 用Jackson提取XML数据
我编写了一个Groovy,其中有一个xml文件作为输入,我想提取xml文件中两个标记的值。这段Java代码适合我:Java 用Jackson提取XML数据,java,xml,dictionary,jackson,Java,Xml,Dictionary,Jackson,我编写了一个Groovy,其中有一个xml文件作为输入,我想提取xml文件中两个标记的值。这段Java代码适合我: 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.
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);
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.InputStream;
导入org.apache.commons.io.IOUtils;
导入org.json.JSONObject;
导入org.json.XML;
导入com.bfi.digi.chk.check汇款;
导入com.fasterxml.jackson.databind.ObjectMapper;
导入com.fasterxml.jackson.databind.SerializationFeature;
check=数据;
InputStream InputStream=新文件InputStream(新文件(
“C:/xml/full_rejection_notification.xml”);
字符串xml=IOUtils.toString(inputStream);
JSONObject jObject=XML.toJSONObject(XML);
ObjectMapper mapper=新的ObjectMapper();
enable(SerializationFeature.INDENT_输出);
Object json=mapper.readValue(jObject.toString(),Object.class);
字符串输出=mapper.writeValueAsString(json);
Map response=newObjectMapper().readValue(输出,HashMap.class);
字符串状态=response.get(“Document”).get(“fitofipmtsrpt”).get(“TxInfAndSts”).get(“TxSts”).toString();
String errorCode=response.get(“Document”).get(“fitofipmtsrpt”).get(“TxInfAndSts”).get(“StsRsnInf”).get(“Rsn”).get(“Cd”).toString();
返回状态.concat(“”).concat(错误代码);
问题是,我得到一个包含重复块的xml文件,并且由于映射不允许重复键,我必须找到另一个解决方案。我正在寻找您的建议。您能不能简单地解析XML文件,然后调用?请参阅,您可以显示xml吗?或者一个描述你的案例的例子?