java xml集合标签_java – 使用JAXB解组具有相同名称的XML标记

我建议你这个解决方案.通过这种方式,您可以根据需要添加任意数量的级别.

Root.java

@XmlAccessorType(XmlAccessType.FIELD)

@XmlType(name = "root", propOrder = {

"items"

})

@XmlRootElement(name = "root")

public class Root

implements Serializable

{

private final static long serialVersionUID = 1234567890L;

@XmlElement(name = "item")

protected List items;

/**

* Gets the value of the items property.

*

*

* This accessor method returns a reference to the live list,

* not a snapshot. Therefore any modification you make to the

* returned list will be present inside the JAXB object.

* This is why there is not a set method for the items property.

*

*

* For example, to add a new item, do as follows:

*

 
 

* getItems().add(newItem);

*

*

*

*

* Objects of the following type(s) are allowed in the list

* {@link Item }

*

*

*/

public List getItems() {

if (items == null) {

items = new ArrayList();

}

return this.items;

}

}

Item.java

@XmlAccessorType(XmlAccessType.FIELD)

@XmlType(name = "item", propOrder = {

"content"

})

@XmlRootElement(name = "item")

public class Item

implements Serializable

{

private final static long serialVersionUID = 1234567890L;

@XmlMixed

@XmlAnyElement(lax = true)

protected List content;

@XmlAttribute(name = "num")

protected String num;

@XmlAttribute(name = "label")

protected String label;

@XmlAttribute(name = "type")

protected String type;

/**

* Gets the value of the content property.

*

*

* This accessor method returns a reference to the live list,

* not a snapshot. Therefore any modification you make to the

* returned list will be present inside the JAXB object.

* This is why there is not a set method for the content property.

*

*

* For example, to add a new item, do as follows:

*

 
 

* getContent().add(newItem);

*

*

*

*

* Objects of the following type(s) are allowed in the list

* {@link String }

* {@link Object }

*

*

*/

public List getContent() {

if (content == null) {

content = new ArrayList();

}

return this.content;

}

/**

* Recupera il valore della proprietà num.

*

* @return

* possible object is

* {@link String }

*

*/

public String getNum() {

return num;

}

/**

* Imposta il valore della proprietà num.

*

* @param value

* allowed object is

* {@link String }

*

*/

public void setNum(String value) {

this.num = value;

}

/**

* Recupera il valore della proprietà label.

*

* @return

* possible object is

* {@link String }

*

*/

public String getLabel() {

return label;

}

/**

* Imposta il valore della proprietà label.

*

* @param value

* allowed object is

* {@link String }

*

*/

public void setLabel(String value) {

this.label = value;

}

/**

* Recupera il valore della proprietà type.

*

* @return

* possible object is

* {@link String }

*

*/

public String getType() {

return type;

}

/**

* Imposta il valore della proprietà type.

*

* @param value

* allowed object is

* {@link String }

*

*/

public void setType(String value) {

this.type = value;

}

}

我用过这个XSD

Main.java

public static void main(String[] args) throws Throwable {

JAXBContext jc = JAXBContext.newInstance(Root.class, Item.class);

Root r = new Root();

Item i = new Item();

i.setLabel("This is a LIST item");

i.setType("List");

Item i2 = new Item();

i2.setLabel("Upper");

i2.setType("string");

i2.getContent().add("ABC");

i.getContent().add(i2);

Item i3 = new Item();

i3.setLabel("Lower");

i3.setType("string");

i3.getContent().add("abc");

i.getContent().add(i3);

Item i4 = new Item();

i4.setNum("1");

i4.setType("list");

Item i5 = new Item();

i5.setLabel("a");

i5.setType("other");

i5.getContent().add("aaaaa");

i4.getContent().add(i5);

i.getContent().add(i4);

r.getItems().add(i);

Marshaller marshaller = jc.createMarshaller();

marshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );

marshaller.marshal(r, System.out);

}

产量

ABC

abc

aaaaa

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
生成SOAP XML可以使用Java的SOAP API来完成,以下是一个简单的示例: ```java // 创建SOAP消息 MessageFactory messageFactory = MessageFactory.newInstance(); SOAPMessage soapMessage = messageFactory.createMessage(); // 创建SOAP部分 SOAPPart soapPart = soapMessage.getSOAPPart(); // 创建SOAP消息体 SOAPBody soapBody = soapPart.getEnvelope().getBody(); SOAPElement soapElement = soapBody.addChildElement("HelloWorld"); // 添加SOAP消息参数 SOAPElement soapElement1 = soapElement.addChildElement("Name"); soapElement1.addTextNode("张三"); // 将SOAP消息转换为XML字符串 ByteArrayOutputStream out = new ByteArrayOutputStream(); soapMessage.writeTo(out); String xml = new String(out.toByteArray()); // 输出SOAP XML字符串 System.out.println(xml); ``` 将SOAP XML组为Java对象可以使用JavaJAXB API来完成,以下是一个简单的示例: ```java // 定义Java对象 @XmlRootElement(name = "HelloWorld") @XmlAccessorType(XmlAccessType.FIELD) public class HelloWorld { @XmlElement(name = "Name") private String name; // getter 和 setter 方法 } // 将SOAP XML析为Java对象 ByteArrayInputStream in = new ByteArrayInputStream(xml.getBytes()); JAXBContext jaxbContext = JAXBContext.newInstance(HelloWorld.class); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); HelloWorld helloWorld = (HelloWorld) unmarshaller.unmarshal(in); // 输出Java对象 System.out.println(helloWorld.getName()); ``` 需要注意的是,析SOAP XML时需要先将SOAP消息部分提取出来,然后再析为Java对象。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值