使用 JAXB 从 XML 字符串创建对象

如何使用下面的代码将 XML 字符串解组,并将其映射到下面的 JAXB 对象?

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;
}
}
277385 次浏览

要传递 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);

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

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

但是通常你是从某个地方得到这个字符串的,例如一个文件。如果是这样的话,最好通过 FileReader本身。

或者,如果你想听一句简单的俏皮话:

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

如果您已经有了 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());
}

方法 getCharterDataFromElement 是

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


return cd.getData();
}
return "";
}
If you want to parse using InputStreams


public Object xmlToObject(String xmlDataString) {
Object converted = null;
try {
        

JAXBContext jc = JAXBContext.newInstance(Response.class);
        

Unmarshaller unmarshaller = jc.createUnmarshaller();
InputStream stream = new ByteArrayInputStream(xmlDataString.getBytes(StandardCharsets.UTF_8));
            

converted = unmarshaller.unmarshal(stream);
} catch (JAXBException e) {
e.printStackTrace();
}
return converted;
}