根据不同的请求读取具有多个值的XML主体元素

问题描述 投票:1回答:1

我正在使用XML解析Java请求。 XML结构是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<TestServices>
    <header>
        //Header Details
    </header>
    <body>
        <ele1>
            <ele2>
                <ele3>534159XXXXXX0176</ele3>  //Or ele_3, ele03, ele_03             
            </ele2>
        </ele1>
    </body>
</TestServices>

我已经创建了相同的类来读取Header和Body元素。每个节点都是一个类,我正在读这个ele3值。

String ele3 = testServicesRequest.getBody().getEle1().getEle2().getEle3();

ele3的元素名称可以根据不同的请求而不同。我在eclipse中使用了xsd功能的Generate Java类,它生成了这样的类。

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "ele3"
})
public class ReqEle2 {
    @XmlElement(name = "ele3", required = true)
    protected String ele3;
    public String getEle3() {
        return ele3;
    }    
    public void setEle3(String value) {
        this.ele3 = value;
    }    
}

我的要求很简单。我只想为单个getEle3()方法添加多个元素名称。例如。 ele_3,ele03,ele_03使用较少的代码更改。或者请建议我任何其他有效的方法来做到这一点。

现在我想这样做,我觉得这不好。

public class ReqEle3 {

    @XmlElement(name = "ele03", required = true)
    protected String ele3_1="";

    @XmlElement(name = "ele_3", required = true)
    protected String ele3_2="";

    @XmlElement(name = "ele3", required = true)
    protected String ele3_3="";

    @XmlElement(name = "ele3_old", required = true)
    protected String ele3_4="";

    public String getEle3() {
        if(ele3_1 != null && !ele3_1.isEmpty()){            
            return ele3_1;  
        }        
        else if(ele3_2 != null && !ele3_2.isEmpty()){           
            return ele3_2;  
        }
        else if(ele3_3 != null && !ele3_3.isEmpty()){           
            return ele3_3;  
        }
        else if(ele3_4 != null && !ele3_4.isEmpty()){           
            return ele3_4;  
        }
        return "";
    }    
}
java xml eclipse xml-parsing javaparser
1个回答
1
投票

您可以为ele3节点编写自定义反序列化器。确切地说,ele2节点的自定义反序列化器,因为这是最后一个常量节点。以下示例仅包含了解解决方案所需的部分:

import org.w3c.dom.Element;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.io.StringReader;

public class JaxbApp {

    public static void main(String[] args) throws Exception {
        JAXBContext jaxbContext = JAXBContext.newInstance(ReqEle1.class);
        String xml0 = "<ele1><ele2><ele3>534159XXXXXX0176</ele3></ele2></ele1>";
        String xml1 = "<ele1><ele2><ele_3>534159XXXXXX0176</ele_3></ele2></ele1>";
        String xml2 = "<ele1><ele2><ele03>534159XXXXXX0176</ele03></ele2></ele1>";

        for (String xml : new String[]{xml0, xml1, xml2}) {
            StringReader reader = new StringReader(xml);
            Object unmarshal = jaxbContext.createUnmarshaller().unmarshal(reader);
            System.out.println(unmarshal);
        }
    }
}

@XmlRootElement(name = "ele1")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"ele2"})
class ReqEle1 {

    @XmlJavaTypeAdapter(ReqEle2XmlAdapter.class)
    @XmlElement(name = "ele2", required = true)
    protected ReqEle2 ele2;

    // getters, setters
}

class ReqEle2XmlAdapter extends XmlAdapter<Object, ReqEle2> {

    @Override
    public ReqEle2 unmarshal(Object v) {
        Element element = (Element) v;
        ReqEle2 reqEle2 = new ReqEle2();
        reqEle2.setEle3(element.getFirstChild().getTextContent());

        return reqEle2;
    }

    @Override
    public Object marshal(ReqEle2 v) throws Exception {
        return null; // Implement if needed
    }
}

class ReqEle2 {

    protected String ele3;

    // getters, setters
}

上面的代码打印:

ReqEle1{ele2=ReqEle2{ele3='534159XXXXXX0176'}}
ReqEle1{ele2=ReqEle2{ele3='534159XXXXXX0176'}}
ReqEle1{ele2=ReqEle2{ele3='534159XXXXXX0176'}}

也可以看看:

© www.soinside.com 2019 - 2024. All rights reserved.