我是解析/编组字符串 xml 到 java 对象的新手。我只想知道如何在字符串 xml 中获取字符串 xml 并转换为 java 对象。
这是我来自 HTTP GET 的字符串 xml:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/"><?xml version="1.0" encoding="utf-8" ?
><MyList><Obj>Im obj 1</Obj><Obj>Im obj
1</Obj></MyList></string>
我注意到stackoverflow正在删除根元素,即“字符串”,只显示
<?xml version="1.0" encoding="utf-8" ?> <MyList> <Obj>Im obj 1</Obj> <Obj>如果我没有将此字符串 xml 放在代码块中,我会立即 obj 2</Obj> </MyList>。
我正在尝试使用 JDom 2,但没有运气。它只获取根元素而不是子元素。
我还使用了 JAXB:
我可以得到根元素,但不能得到子元素。这是我的代码:
JAXBContext jc = JAXBContext.newInstance(myPackage.String.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
JAXBElement<MyList> jaxbElement = unmarshaller.unmarshal(new StreamSource(new
ByteArrayInputStream(byteArray)), MyList.class);
System.out.println(jaxbElement.getClass()); --> this will print myPackage.MyList
MyList myList = (MyList) jaxbElement.getValue();
System.out.println("myList.Obj = " + myList.getObjs().size()); --> this will return 0