具有xml属性的java中的XML到JSON转换

时间:2016-12-09 08:45:37

标签: java json xml gson

我有一个带有xml属性的xml,如下所示。现在当我将这个xml转换为JSON时,由于BvdState中存在一个属性,它失败了。我不知道该怎么处理?

<State>
    <Number>3</Number>
    <Name>MotherBIG</Name>
    <BvdState i:nil="true"/>
    <HistoryState>3</HistoryState>
</State>

1 个答案:

答案 0 :(得分:1)

您可以通过这种方式处理此属性 -

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。使用它时,你只想创建一个xml结构的pojo类,而不仅仅是这个。

看看下面的代码 -

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