gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:43:44 25 4
gpt4 key购买 nike

我正在使用 Java 解析 XML 请求。 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 中使用了 Generate Java class from xsd 功能,它生成了这样的类。

@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 "";
}
}

最佳答案

您可以为 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'}}

另请参阅:

关于java - 根据不同的请求读取具有多个值的XML主体元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55204289/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com