Answer the question
In order to leave comments, you need to log in
How to add "@" prefix to attribute of xml tag when converting to json?
There is a code that receives an xml stream, translates it into json and returns a json string
public static String xmlToJson(InputStream inputStream) {
try {
InputFactoryImpl inputFactory = new InputFactoryImpl();
inputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE);
OutputFactoryImpl outputFactory = new OutputFactoryImpl();
outputFactory.setProperty(XMLStreamProperties.XSP_NAMESPACE_AWARE, Boolean.FALSE);
XmlMapper xmlMapper = new XmlMapper(new XmlFactory(inputFactory,outputFactory), new JacksonXmlModule());
xmlMapper.enable(SerializationFeature.INDENT_OUTPUT);
JsonNode node = xmlMapper.readTree(inputStream);
ObjectMapper jsonMapper = new ObjectMapper();
return jsonMapper.writeValueAsString(node);
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question