gpt4 book ai didi

java - 使用 xml 属性在 java 中进行 XML 到 JSON 的转换

转载 作者:行者123 更新时间:2023-12-01 09:08:48 26 4
gpt4 key购买 nike

我有一个 xml,其 xml 属性如下。现在,当我将此 xml 转换为 JSON 时,由于 BvdState 中存在属性,它会失败。我不知道如何处理这个问题?

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

最佳答案

你可以这样处理这个属性 -

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

关于java - 使用 xml 属性在 java 中进行 XML 到 JSON 的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41056382/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com