0

I have one xml with xml attributes as below. Now when i convert this xml to JSON it fails because of an attribute present into BvdState. I dont know how to deal with this ?

<State>
    <Number>3</Number>
    <Name>MotherBIG</Name>
    <BvdState i:nil="true"/>
    <HistoryState>3</HistoryState>
</State>
user34567
  • 258
  • 3
  • 12
  • @user34567- please look at http://fasterxml.github.io/jackson-dataformat-xml/javadoc/2.7/com/fasterxml/jackson/dataformat/xml/XmlMapper.html?is-external=true – ketan Dec 09 '16 at 08:58
  • You can check this similar question: http://stackoverflow.com/questions/27648774/convert-an-xml-file-to-json-using-using-gson – sanastasiadis Dec 09 '16 at 11:48

1 Answers1

1

You can deal with this attribute in this way -

inputStream = XMLtoJsonConverter.class.getClassLoader().getResourceAsStream("simple.xml");
String xml = IOUtils.toString(inputStream);
System.out.println(org.json.XML.toJSONObject(xml).toString(4));

If you want to go into depth of xml to json serialization look at this. while using this, you just want to create a pojo classes of xml structure nothing more than that.

Take a look at below code -

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;
    }
}
ketan
  • 2,732
  • 11
  • 34
  • 80
  • @user34567- If you find any usefull info in my answer then give reputation to my answer or you find it is a correct answer then don't forget to mark as a right answer. – ketan Dec 09 '16 at 12:16