本文介绍了如何根据我的要求使用JAXB XML Adapter感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JAXB进行解组过程,请求来自UI到我们的服务类。以下是XML请求的格式。

I am Using JAXB for unmarshalling process , for which the request comes from the UI to our service class . The below is the format of XML request .

<SampleRequest  user="testUser"  account="testAccount"    Specifier=  "value1a,value1b,value1c : name2a,value2b,value2c"/>

我的要求是,Specifier属性有多个值系列(:冒号分隔)我需要将每个值系列映射到我的自定义java类

My requirement is that , the Specifier attribute has got Multiple series of values (: colon separated) i need to map each series of values to my custom java class

我试过这种方式

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class SampleRequest {

    @XmlAttribute
    private String user;

    @XmlAttribute
    private String account;


    @XmlAttribute(name="Specifier")
    private List<Specifier> specifier;


}

Specifier.java

@XmlJavaTypeAdapter(SpecifierAdapter.class)
public class Specifier {



}

SpecifierAdapter.java

public class SpecifierAdapter  extends XmlAdapter{

    @Override
    public Object marshal(Object arg0) throws Exception {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Object unmarshal(Object arg0) throws Exception {
        // TODO Auto-generated method stub
        return null;
    }

}

编辑部分

类Specifier有3个String属性。

class Specifier
{
String value1;
String value2;
String value3;
}

我需要每个系列的Specifier例如(value1a,value1b) ,value1c)应分别映射到value1,value2,value3

编辑第3部分


感谢您的回复,我试图解开这个例子,我发现的是,我得到了空白

这是我通过的请求

<sampleRequest user="user" account="account" Specifier="v1,v2,v3 : a1,a2,a3"/>

只是想确保,我的Specifier类是否正确? (因为我这里没有使用任何注释)

package com;

import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlJavaTypeAdapter(SpecifierAdapter.class)
public class Specifier {


    Specifier(String v1 , String v2 , String v3)
    {

    }

    String value1;

    public String getValue1() {
        return value1;
    }

    public void setValue1(String value1) {
        this.value1 = value1;
    }

    public String getValue2() {
        return value2;
    }

    public void setValue2(String value2) {
        this.value2 = value2;
    }

    public String getValue3() {
        return value3;
    }

    public void setValue3(String value3) {
        this.value3 = value3;
    }

    String value2;
    String value3;

}


推荐答案

注意:使用guava-library的可以使适配器代码更简单。

NOTE: The adapter code could be made simpler by using guava-library's Joiner and Splitter.

SampleRequest

public class SampleRequest
{
    @XmlAttribute
    private String user;

    @XmlAttribute
    private String account;

    @XmlAttribute(name = "Specifier")
    @XmlJavaTypeAdapter(SpecifierAdapter.class)
    private List<Specifier> specifier;
}

适配器

public class SpecifierAdapter extends XmlAdapter<String, List<Specifier>>
{
    @Override
    public List<Specifier> unmarshal(final String v) throws Exception
    {
        String[] values = v.split(":");
        List<Specifier> l = new ArrayList<Specifier>();
        for (String s : values)
        {
            String[] vs = s.split(",");
            l.add(new Specifier(vs[0], vs[1], vs[2]));
        }
        return l;
    }

    @Override
    public String marshal(final List<Specifier> v) throws Exception
    {
        String values = "";
        for (Specifier s : v)
        {
            values += s.getValue1() + "," + s.getValue2() + "," + s.getValue3() + " : ";
        }
        return values.length() > 0 ? values.substring(0, values.length() - 3) : values;
    }
}

用法

public static void main(final String a[]) throws JAXBException
{
        SampleRequest r = new SampleRequest();
        r.setAccount("account");
        r.setUser("user");
        List<Specifier> sps = new ArrayList<Specifier>();
        sps.add(new Specifier("v1", "v2", "v3"));
        sps.add(new Specifier("a1", "a2", "a3"));
        r.setSpecifier(sps);

        JAXBContext jc = JAXBContext.newInstance(SampleRequest.class);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(r, System.out);
}

输出

<?xml version="1.0" encoding="UTF-8"?>
<sampleRequest user="user" account="account" Specifier="v1,v2,v3 : a1,a2,a3"/>

这篇关于如何根据我的要求使用JAXB XML Adapter感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 10:19