如何将XML字符串转换为Java对象或JSON对象。

huangapple go评论62阅读模式
英文:

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.

&lt;XMLRequest&gt;
    &lt;RequestAction&gt;Modify&lt;/RequestAction&gt;
    &lt;MerchantDetail&gt;
        &lt;CloverID&gt;10&lt;/CloverID&gt;
        &lt;MerchantNumber&gt;111111&lt;/MerchantNumber&gt;
        &lt;Platform&gt;N&lt;/Platform&gt;
        &lt;DBAName&gt;Test_306&lt;/DBAName&gt;
        &lt;LegalName&gt;Test_306&lt;/LegalName&gt;
        &lt;Address1&gt;Fountain road&lt;/Address1&gt;
        &lt;City&gt;Vista&lt;/City&gt;
        &lt;State&gt;Boulder&lt;/State&gt;
        &lt;Zip&gt;1518580&lt;/Zip&gt;
        &lt;Country&gt;US&lt;/Country&gt;
        &lt;PhoneNumber&gt;1234567890&lt;/PhoneNumber&gt;
        &lt;Email&gt;test@gmail.com&lt;/Email&gt;
        &lt;Contact&gt;Test_306&lt;/Contact&gt;
        &lt;Reseller&gt;DevelopersUS&lt;/Reseller&gt;
        &lt;MCCCode&gt;7394&lt;/MCCCode&gt;
        &lt;Currency&gt;USD&lt;/Currency&gt;
        &lt;TAEncryptionType&gt;0001&lt;/TAEncryptionType&gt;
        &lt;GroupID&gt;10001&lt;/GroupID&gt;
        &lt;SupportPhone&gt;1234567890&lt;/SupportPhone&gt;
        &lt;ABAAccountNumber&gt;000000000000000&lt;/ABAAccountNumber&gt;
        &lt;DDAAccountNumber&gt;***********3770&lt;/DDAAccountNumber&gt;
    &lt;/MerchantDetail&gt;
    &lt;CardTypes&gt;
        &lt;CardType CardName = &quot;MASTER&quot;&gt;
            &lt;SENUMBER /&gt;
            &lt;AlphaID&gt;1234&lt;/AlphaID&gt;
            &lt;ServiceType&gt;F&lt;/ServiceType&gt;
        &lt;/CardType&gt;
        &lt;CardType CardName = &quot;VISA&quot;&gt;
            &lt;SENUMBER /&gt;
            &lt;AlphaID /&gt;
            &lt;ServiceType /&gt;
        &lt;/CardType&gt;
    &lt;/CardTypes&gt;
&lt;/XMLRequest&gt;

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 = &quot;XMLRequest&quot;)
@XmlAccessorType(XmlAccessType.FIELD)
public class XMLRequest {
@XmlElement(name = &quot;RequestAction&quot;)
private String requestAction;
@XmlElement(name = &quot;MerchantDetail&quot;)
private MerchantDetail merchantDetail;
@XmlElementWrapper(name = &quot;CardTypes&quot;)
@XmlElement(name = &quot;CardType&quot;)
private CardType[] cardTypes;

}

and MerchantDetail class

    @XmlAccessorType(XmlAccessType.FIELD)
public class MerchantDetail {
@XmlElement(name = &quot;CloverID&quot;)
private int cloverID;
@XmlElement(name = &quot;MerchantNumber&quot;)
private int merchantNumber;
@XmlElement(name = &quot;Platform&quot;)
private String platform;
@XmlElement(name = &quot;DBAName&quot;)
private String dbaName;
@XmlElement(name = &quot;LegalName&quot;)
private String legalName;
@XmlElement(name = &quot;Address1&quot;)
private String address1;
@XmlElement(name = &quot;City&quot;)
private String city;
@XmlElement(name = &quot;State&quot;)
private String state;
@XmlElement(name = &quot;Zip&quot;)
private String zip;
@XmlElement(name = &quot;Country&quot;)
private String country;
@XmlElement(name = &quot;PhoneNumber&quot;)
private String phoneNumber;
@XmlElement(name = &quot;Email&quot;)
private String email;
@XmlElement(name = &quot;Contact&quot;)
private String contact;
@XmlElement(name = &quot;Reseller&quot;)
private String reseller;
@XmlElement(name = &quot;MCCCode&quot;)
private int mccCode;
@XmlElement(name = &quot;Currency&quot;)
private String currency;
@XmlElement(name = &quot;TAEncryptionType&quot;)
private String taEncryptionType;
@XmlElement(name = &quot;GroupID&quot;)
private int groupID;
@XmlElement(name = &quot;SupportPhone&quot;)
private String supportPhone;
@XmlElement(name = &quot;ABAAccountNumber&quot;)
private String abaAccountNumber;
@XmlElement(name = &quot;DDAAccountNumber&quot;)
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(&quot;input.xml&quot;));

huangapple
  • 本文由 发表于 2023年4月4日 13:23:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75925765.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定