Вы можете иметь дело с этим атрибутом таким образом -
inputStream = XMLtoJsonConverter.class.getClassLoader().getResourceAsStream("simple.xml");
String xml = IOUtils.toString(inputStream);
System.out.println(org.json.XML.toJSONObject(xml).toString(4));
Если вы хотите идти в глубину XML в JSON сериализации взгляд на this. используя это, вы просто хотите создать классы pojo структуры xml не более того.
Взгляните ниже код -
JacksonDeserializer.java -
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import java.io.IOException;
import java.net.URL;
import java.util.List;
public class JacksonDeserializer {
private List<Item> item;
private XmlMapper xmlMapper = null;
private SimpleModule module = null;
private Channel ch = null;
public List<Item> getItem() {
return item;
}
public void setItem(List<Item> item) {
this.item = item;
}
public void readXML() throws IOException {
xmlMapper = new XmlMapper();
module = new SimpleModule();
ch = new Channel();
module.addDeserializer(List.class, ch.new ChannelDeserializer());
xmlMapper.registerModule(module);
xmlMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
URL url = new URL("some xml data available on url");
Rss rss = xmlMapper.readValue(url, Rss.class);//you can provide xml file also
Channel channel = rss.getChannel();
JacksonDeserializer obj = new JacksonDeserializer();
item = channel.getItem();
obj.setItem(item);
}
}
Rss.java -
public class Rss {
private Channel channel;
public Rss() {
}
public Channel getChannel() {
return channel;
}
public void setChannel(Channel channel) {
this.channel = channel;
}
}
Channel.java -
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Channel {
private List<Item> item = new ArrayList();
public Channel() {
}
public List<Item> getItem() {
return item;
}
public void setItem(List<Item> item) {
this.item = item;
}
public class ChannelDeserializer extends JsonDeserializer<List<Item>> {
@Override
public List<Item> deserialize(JsonParser jp, DeserializationContext arg1) throws IOException, JsonProcessingException {
JsonNode jsonNode = jp.getCodec().readTree(jp);
String title = jsonNode.get("title").asText();
String link = jsonNode.get("link").asText();
String description = jsonNode.get("description").asText();
String pubDate = jsonNode.get("pubDate").asText();
String source = jsonNode.get("source").path("").asText();
Item i = new Item(title, link, description, pubDate, source);
item.add(i);
return item;
}
}
}
Item.java -
public class Item {
private String title;
private String link;
private String description;
private String pubDate;
private String source;
public Item() {
}
public Item(String title, String link, String description, String pubDate, String source) {
this.title = title;
this.link = link;
this.description = description;
this.pubDate = pubDate;
this.source = source;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPubDate() {
return pubDate;
}
public void setPubDate(String pubDate) {
this.pubDate = pubDate;
}
}
@ user34567- пожалуйста, смотрите на http://fasterxml.github.io/jackson-dataformat-xml/javadoc/2.7/com/fasterxml/jackson/dataformat /xml/XmlMapper.html?is-external=true – kit
Вы можете проверить этот похожий вопрос: http://stackoverflow.com/questions/27648774/convert-an-xml-file-to-json-using-using-gson – sanastasiadis