Ich habe folgende Methode, die auch funktioniert:
Das ist EmployeeDTO:
Das ist DepartmentDTO:
Nun möchte ich aber so ein XML parsen:
Java:
package com.x.y.z.backend.helper;
import com.x.y.z.shared.client.test.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;
public class ReadWriteXML {
private static final Logger logger = LoggerFactory.getLogger(ReadWriteXML.class);
public static void myMethod() {
String xmlString = "" +
"<employee>" +
" <department>" +
" <id>101</id>" +
" <name>IT</name>" +
" </department>" +
" <id>1</id>" +
" <lastName>Mayer</lastName>" +
"</employee>";
JAXBContext jaxbContext;
try {
jaxbContext = JAXBContext.newInstance(EmployeeDTO.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
EmployeeDTO d = (EmployeeDTO) jaxbUnmarshaller.unmarshal(new StringReader(xmlString));
} catch (Exception e) {
e.printStackTrace();
}
}
}
Das ist EmployeeDTO:
Java:
package com.x.y.z.shared.client.test;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;
@XmlRootElement(name = "employee")
@XmlAccessorType(XmlAccessType.FIELD)
public class EmployeeDTO implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String lastName;
private DepartmentDTO department;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public DepartmentDTO getDepartment() {
return department;
}
public void setDepartment(DepartmentDTO department) {
this.department = department;
}
}
Das ist DepartmentDTO:
Java:
package com.x.y.z.shared.client.test;
import com.x.y.z.shared.client.SharedFunctions;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;
@XmlRootElement(name = "department")
@XmlAccessorType(XmlAccessType.FIELD)
public class DepartmentDTO implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Nun möchte ich aber so ein XML parsen:
XML:
<abc>
<path d="Hallo" id="path5306" />
<text xml:space="preserve" id="text5310">
<tspan y="0" id="tspan5308">01.01</tspan>
</text>
<text xml:space="preserve" id="text5311">
<tspan y="0" id="tspan5309">01.02</tspan>
</text>
</abc>
Zuletzt bearbeitet: