我想将具有配置属性的XML文件解析为JSON,并将所有JSON转换为最终结果对象。
我的课程如下:
@SpringBootApplication
public class AdvancedApplication {
public static void main(String[] args) {
SpringApplication.run(AdvancedApplication.class, args);
XmlMapper xmlMapper = new XmlMapper();
try {
List XMLEntries = xmlMapper
.readValue(new ClassPathResource("configuration.xml")
.getFile(), List.class);
ObjectMapper mapper = new ObjectMapper();
String jsonConfig = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(XMLEntries);
JsonNode parent = new ObjectMapper().readTree(jsonConfig);
String content = parent.path("serverport").asText();
System.out.println(content);
System.out.println(jsonConfig);
} catch (IOException e) {
e.printStackTrace();
}
}
}
在第一种情况下:
List XMLEntries = xmlMapper
.readValue(new ClassPathResource("configuration.xml")
.getFile(), List.class);
上述方法将JSON包装在列表中,结果如下:
[ {
"serverport" : "9966"
}, {
"clientport" : "9999",
"serverHost" : "localhost"
} ]
但是在这种情况下,我无法通过以下行读取值:
String content = parent.path("serverport").asText();
因为内容为空。
最后,我决定以这种特殊方式将JSON转换为结果对象Config:
Config configObject = mapper.readValue(jsonConfig, Config.class);
但不幸的是,我收到了如下异常:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.javase.advanced.config.Config` out of START_ARRAY token
at [Source: (String)"[ {
"serverport" : "9966"
}, {
"clientport" : "9999",
"serverHost" : "localhost"
} ]"; line: 1, column: 1]
我的configuration.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<server serverport="9966"/>
<client clientport="9999">
<serverHost>localhost</serverHost>
</client>
</config>
和配置类如下:
@NoArgsConstructor
@Getter
@AllArgsConstructor
@ToString
public class Config {
private Server server;
private Client client;
}
Server.class
@AllArgsConstructor
@Getter
@NoArgsConstructor
public class Server {
@JsonProperty("serverport")
private String serverPort;
}
Config.class
@Getter
@NoArgsConstructor
public class Client {
@JsonProperty("serverHost")
private String serverHost;
@JsonProperty("clientport")
private String clientPort;
}
我要实现的是将configuration.xml文件解析为JSON,并将其转换为Config对象,以创建配置类以供进一步使用。
编辑:
在提出@pvpkiran
建议之后,我尝试了以下方法:
Config configValue = xmlMapper.readValue(new ClassPathResource("configuration.xml")
.getFile(), Config.class);
但是毕竟我收到的错误是:
org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "serverport" (Class com.javase.advanced.server.Server), not marked as ignorable
at [Source: C:\Users\mzawadzki\Desktop\advanced\target\classes\configuration.xml; line: 3, column: 30] (through reference chain: com.javase.advanced.config.Config["server"]->com.javase.advanced.server.Server["serverport"])
EDIT2:
修改后,我的类如下:
Client.class
@Getter
@NoArgsConstructor
public class Client {
@JacksonXmlProperty(localName = "clientport")
private String clientPort;
@JacksonXmlProperty(localName = "serverHost")
private String serverHost;
}
Server.class
@AllArgsConstructor
@Getter
@NoArgsConstructor
public class Server {
@JacksonXmlProperty(localName = "serverport")
private String serverPort;
}
我为我的配置类创建了一个包装器,以消除[
和]
的出现。
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
@JacksonXmlRootElement(localName = "config")
public class ConfigWrapper {
private Config[] configurations;
}
现在我的配置类如下:
@NoArgsConstructor
@Getter
@AllArgsConstructor
@ToString
public class Config {
@JacksonXmlProperty(localName = "serverport")
private Server server;
@JacksonXmlProperty(localName = "client")
private Client client;
}
但还是不能解决问题,毕竟我收到了异常:
org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "server" (Class com.javase.advanced.config.ConfigWrapper), not marked as ignorable
at [Source: (through reference chain: com.javase.advanced.config.ConfigWrapper["server"])
我将非常感谢您提出关于如何达到目标并消除这种例外的建议。
答案 0 :(得分:2)
这应该有效。您可以使用JAXB进行编组。阅读有关JAXB的内容。还要注意如何使用XmlElement
和XmlAttribute
。
String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<config>\n" +
" <server serverport=\"9966\"/>\n" +
" <client clientport=\"9999\">\n" +
" <serverHost>localhost</serverHost>\n" +
" </client>\n" +
"</config>";
JAXBContext jaxbContext;
try
{
jaxbContext = JAXBContext.newInstance(Config.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Config config = (Config) jaxbUnmarshaller.unmarshal(new StringReader(xmlString));
System.out.println(config);
}catch (JAXBException e){
e.printStackTrace();
}
Config类将是这样
@XmlRootElement(name = "config")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Config {
public Server server;
public Client client;
public Config() {
}
public Server getServer() {
return server;
}
public void setServer(Server server) {
this.server = server;
}
public Client getClient() {
return client;
}
public void setClient(Client client) {
this.client = client;
}
}
服务器类
public class Server {
@XmlAttribute(name = "serverport")
public String serverPort;
}
客户端类
public class Client {
@XmlAttribute(name = "clientport")
public String clientPort;
@XmlElement
public String serverHost;
}
答案 1 :(得分:0)
pvpkiran
的解决方案解决了一个问题,但是我无法忍受Jackson不能将XML解析为单个对象的事实。调查带来了令人满意的效果,最终证明我的pom.XML中有两个类似的依赖项,例如jackson-databind
和jackson-xml-databind
。原来它们之间存在冲突。注释掉jackson-xml-databind
后,一切正常。
现在我的课程如下:
Client.class
@Getter
@NoArgsConstructor
@Setter
public class Client {
@JacksonXmlProperty(localName = "clientport")
private String clientPort;
@JacksonXmlProperty(localName = "serverHost")
private String serverHost;
}
Server.class
@AllArgsConstructor
@Getter
@NoArgsConstructor
@Setter
public class Server {
@JacksonXmlProperty(localName = "serverport")
private String serverPort;
}
Config.class
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@ToString
@JacksonXmlRootElement(localName = "config")
public class Config {
private Server server;
private Client client;
}
再次感谢您的承诺。