英文:
How to convert to XMl string to java object or json object
问题
我有一个XML字符串,我想将其转换为Java对象。
<XMLRequest>
<RequestAction>Modify</RequestAction>
<MerchantDetail>
<CloverID>10</CloverID>
<MerchantNumber>111111</MerchantNumber>
<Platform>N</Platform>
<DBAName>Test_306</DBAName>
<LegalName>Test_306</LegalName>
<Address1>Fountain road</Address1>
<City>Vista</City>
<State>Boulder</State>
<Zip>1518580</Zip>
<Country>US</Country>
<PhoneNumber>1234567890</PhoneNumber>
<Email>test@gmail.com</Email>
<Contact>Test_306</Contact>
<Reseller>DevelopersUS</Reseller>
<MCCCode>7394</MCCCode>
<Currency>USD</Currency>
<TAEncryptionType>0001</TAEncryptionType>
<GroupID>10001</GroupID>
<SupportPhone>1234567890</SupportPhone>
<ABAAccountNumber>000000000000000</ABAAccountNumber>
<DDAAccountNumber>***********3770</DDAAccountNumber>
</MerchantDetail>
<CardTypes>
<CardType CardName="MASTER">
<SENUMBER />
<AlphaID>1234</AlphaID>
<ServiceType>F</ServiceType>
</CardType>
<CardType CardName="VISA">
<SENUMBER />
<AlphaID />
<ServiceType />
</CardType>
</CardTypes>
</XMLRequest>
我尝试使用以下代码。并创建了相应的POJO类。但是我遇到了以下错误。请帮忙看看我漏掉了什么。
代码:
ObjectMapper xmlMapper = new XmlMapper();
Request value = xmlMapper.readValue(xmlContent, Request.class);
System.out.println(xmlMapper.writeValueAsString(value));
错误: Exception in thread "main" java.lang.NoSuchMethodError:
'com.fasterxml.jackson.core.io.ContentReference
com.fasterxml.jackson.dataformat.xml.XmlFactory._createContentReference(java.lang.Object)'
在
com.fasterxml.jackson.dataformat.xml.XmlFactory.createParser(XmlFactory.java:492)
在
com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3677)
在
com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3645)
在
com.clover.myspringbootapp.example.XmlToJsonObject.main(XmlToJsonObject.java:25)
英文:
I have a XML string, I want to convert to XML to java object.
<XMLRequest>
<RequestAction>Modify</RequestAction>
<MerchantDetail>
<CloverID>10</CloverID>
<MerchantNumber>111111</MerchantNumber>
<Platform>N</Platform>
<DBAName>Test_306</DBAName>
<LegalName>Test_306</LegalName>
<Address1>Fountain road</Address1>
<City>Vista</City>
<State>Boulder</State>
<Zip>1518580</Zip>
<Country>US</Country>
<PhoneNumber>1234567890</PhoneNumber>
<Email>test@gmail.com</Email>
<Contact>Test_306</Contact>
<Reseller>DevelopersUS</Reseller>
<MCCCode>7394</MCCCode>
<Currency>USD</Currency>
<TAEncryptionType>0001</TAEncryptionType>
<GroupID>10001</GroupID>
<SupportPhone>1234567890</SupportPhone>
<ABAAccountNumber>000000000000000</ABAAccountNumber>
<DDAAccountNumber>***********3770</DDAAccountNumber>
</MerchantDetail>
<CardTypes>
<CardType CardName = "MASTER">
<SENUMBER />
<AlphaID>1234</AlphaID>
<ServiceType>F</ServiceType>
</CardType>
<CardType CardName = "VISA">
<SENUMBER />
<AlphaID />
<ServiceType />
</CardType>
</CardTypes>
</XMLRequest>
I tried using below code. And also created respective pojos as well. But I am getting below error. Please help, what I missed.
Code:
ObjectMapper xmlMapper = new XmlMapper();
Request value = xmlMapper.readValue(xmlContent, Request.class);
System.out.println(xmlMapper.writeValueAsString(value));
> Error: Exception in thread "main" java.lang.NoSuchMethodError:
> 'com.fasterxml.jackson.core.io.ContentReference
> com.fasterxml.jackson.dataformat.xml.XmlFactory._createContentReference(java.lang.Object)'
> at
> com.fasterxml.jackson.dataformat.xml.XmlFactory.createParser(XmlFactory.java:492)
> at
> com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3677)
> at
> com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3645)
> at
> com.clover.myspringbootapp.example.XmlToJsonObject.main(XmlToJsonObject.java:25)
答案1
得分: 1
尝试使用类库,例如JAXB,并编写类:
@XmlRootElement(name = "XMLRequest")
@XmlAccessorType(XmlAccessType.FIELD)
public class XMLRequest {
@XmlElement(name = "RequestAction")
private String requestAction;
@XmlElement(name = "MerchantDetail")
private MerchantDetail merchantDetail;
@XmlElementWrapper(name = "CardTypes")
@XmlElement(name = "CardType")
private CardType[] cardTypes;
}
和MerchantDetail类:
@XmlAccessorType(XmlAccessType.FIELD)
public class MerchantDetail {
@XmlElement(name = "CloverID")
private int cloverID;
@XmlElement(name = "MerchantNumber")
private int merchantNumber;
@XmlElement(name = "Platform")
private String platform;
@XmlElement(name = "DBAName")
private String dbaName;
@XmlElement(name = "LegalName")
private String legalName;
@XmlElement(name = "Address1")
private String address1;
@XmlElement(name = "City")
private String city;
@XmlElement(name = "State")
private String state;
@XmlElement(name = "Zip")
private String zip;
@XmlElement(name = "Country")
private String country;
@XmlElement(name = "PhoneNumber")
private String phoneNumber;
@XmlElement(name = "Email")
private String email;
@XmlElement(name = "Contact")
private String contact;
@XmlElement(name = "Reseller")
private String reseller;
@XmlElement(name = "MCCCode")
private int mccCode;
@XmlElement(name = "Currency")
private String currency;
@XmlElement(name = "TAEncryptionType")
private String taEncryptionType;
@XmlElement(name = "GroupID")
private int groupID;
@XmlElement(name = "SupportPhone")
private String supportPhone;
@XmlElement(name = "ABAAccountNumber")
private String abaAccountNumber;
@XmlElement(name = "DDAAccountNumber")
private String ddaAccountNumber;
// 添加getter和setter
}
同样的,CardType类也类似。
然后:
JAXBContext context = JAXBContext.newInstance(XMLRequest.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
XMLRequest xmlRequest = (XMLRequest) unmarshaller.unmarshal(new File("input.xml"));
请注意,这是Java代码示例,用于处理XML数据与Java对象之间的映射。
英文:
Try using library such as JAXB and write class
@XmlRootElement(name = "XMLRequest")
@XmlAccessorType(XmlAccessType.FIELD)
public class XMLRequest {
@XmlElement(name = "RequestAction")
private String requestAction;
@XmlElement(name = "MerchantDetail")
private MerchantDetail merchantDetail;
@XmlElementWrapper(name = "CardTypes")
@XmlElement(name = "CardType")
private CardType[] cardTypes;
}
and MerchantDetail class
@XmlAccessorType(XmlAccessType.FIELD)
public class MerchantDetail {
@XmlElement(name = "CloverID")
private int cloverID;
@XmlElement(name = "MerchantNumber")
private int merchantNumber;
@XmlElement(name = "Platform")
private String platform;
@XmlElement(name = "DBAName")
private String dbaName;
@XmlElement(name = "LegalName")
private String legalName;
@XmlElement(name = "Address1")
private String address1;
@XmlElement(name = "City")
private String city;
@XmlElement(name = "State")
private String state;
@XmlElement(name = "Zip")
private String zip;
@XmlElement(name = "Country")
private String country;
@XmlElement(name = "PhoneNumber")
private String phoneNumber;
@XmlElement(name = "Email")
private String email;
@XmlElement(name = "Contact")
private String contact;
@XmlElement(name = "Reseller")
private String reseller;
@XmlElement(name = "MCCCode")
private int mccCode;
@XmlElement(name = "Currency")
private String currency;
@XmlElement(name = "TAEncryptionType")
private String taEncryptionType;
@XmlElement(name = "GroupID")
private int groupID;
@XmlElement(name = "SupportPhone")
private String supportPhone;
@XmlElement(name = "ABAAccountNumber")
private String abaAccountNumber;
@XmlElement(name = "DDAAccountNumber")
private String ddaAccountNumber;
// add getter and setters
}
Like wise CardType class
then
JAXBContext context = JAXBContext.newInstance(XMLRequest.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
XMLRequest xmlRequest = (XMLRequest) unmarshaller.unmarshal(new File("input.xml"));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论