2011-03-28 39 views
132

如何使用下面的代码将XML字符串映射到下面的JAXB对象?使用JAXB从XML字符串创建对象

JAXBContext jaxbContext = JAXBContext.newInstance(Person.class); 
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); 
Person person = (Person) unmarshaller.unmarshal("xml string here"); 

@XmlRootElement(name = "Person") 
public class Person { 
    @XmlElement(name = "First-Name") 
    String firstName; 
    @XmlElement(name = "Last-Name") 
    String lastName; 
    public String getFirstName() { 
     return firstName; 
    } 
    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 
    public String getLastName() { 
     return lastName; 
    } 
    public void setLastName(String lastName) { 
     this.lastName = lastName; 
    } 
} 

回答

222

要通过XML内容,你需要用一个Reader内容,并解组,与其:

JAXBContext jaxbContext = JAXBContext.newInstance(Person.class); 
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); 

StringReader reader = new StringReader("xml string here"); 
Person person = (Person) unmarshaller.unmarshal(reader); 
+4

如果“xml string here”包含SOAP信封,您可以扩展此答案以包含吗? – JWiley 2014-03-11 14:25:21

+0

如果您想将'Reader'与特定的bean类结合使用,该怎么办?由于没有'unmarshall(Reader,Class)'方法。例如。有没有办法将'Reader'转换成'javax.xml.transform.Source'? – bvdb 2016-07-13 10:29:09

+0

在我的情况下工作如下:'JAXBElement elemento =(JAXBElement )unmarshaller.unmarshal(reader); MyObject object = elemento.getValue();' – 2017-01-12 00:26:42

20

没有unmarshal(String)方法。您应该使用Reader

Person person = (Person) unmarshaller.unmarshal(new StringReader("xml string")); 

但通常你得到该字符串从什么地方,例如文件。如果是这种情况,最好通过FileReader本身。

119

或者,如果你想要一个简单的一行:

Person person = JAXB.unmarshal(new StringReader("<?xml ..."), Person.class); 
+1

这应该是被接受的答案。这有点复杂。 – bobbel 2016-04-22 12:08:31

+0

非常简单。我完全同意,它一定是被接受的答案。 – Afaria 2016-06-17 15:31:31

+2

我实际上不同意上面的评论。它当然更容易,但是它会即时创建上下文,因此即使上下文最终被缓存,它也会对性能产生影响。谨慎使用。 – Crystark 2017-02-02 16:02:23

3

如果您已经拥有了XML,并配备多个属性,你可以按如下处理:

String output = "<ciudads><ciudad><idCiudad>1</idCiudad> 
<nomCiudad>BOGOTA</nomCiudad></ciudad><ciudad><idCiudad>6</idCiudad> 
<nomCiudad>Pereira</nomCiudad></ciudads>"; 
DocumentBuilder db = DocumentBuilderFactory.newInstance() 
    .newDocumentBuilder(); 
InputSource is = new InputSource(); 
is.setCharacterStream(new StringReader(output)); 

Document doc = db.parse(is); 
NodeList nodes = ((org.w3c.dom.Document) doc) 
    .getElementsByTagName("ciudad"); 

for (int i = 0; i < nodes.getLength(); i++) {   
    Ciudad ciudad = new Ciudad(); 
    Element element = (Element) nodes.item(i); 

    NodeList name = element.getElementsByTagName("idCiudad"); 
    Element element2 = (Element) name.item(0); 
    ciudad.setIdCiudad(Integer 
     .valueOf(getCharacterDataFromElement(element2))); 

    NodeList title = element.getElementsByTagName("nomCiudad"); 
    element2 = (Element) title.item(0); 
    ciudad.setNombre(getCharacterDataFromElement(element2)); 

    ciudades.getPartnerAccount().add(ciudad); 
} 
} 

for (Ciudad ciudad1 : ciudades.getPartnerAccount()) { 
System.out.println(ciudad1.getIdCiudad()); 
System.out.println(ciudad1.getNombre()); 
} 

的方法getCharacterDataFromElement是

public static String getCharacterDataFromElement(Element e) { 
Node child = e.getFirstChild(); 
if (child instanceof CharacterData) { 
CharacterData cd = (CharacterData) child; 

return cd.getData(); 
} 
return ""; 
}