Answer the question
In order to leave comments, you need to log in
Why can't parse xml?
Hello. A string comes to my program in the form of an xml document. I need to parse it (turn it into the necessary objects). I am using basic JAXB. I placed all the comments, wrote the necessary code, but for some reason it does not parse the necessary object. XML example:
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<documentrequest type="staff" mode_display="employ_and_dismiss" subdiv="">
<staff>
<staffnode last_name="Бердников" first_name="Дмитрий" middle_name="" id_internal="1333771" id_external=""
tabel_id="0000000000" date_begin="01.01.1980" date_action="01.01.1980" id_subdiv_external=""
id_appoint_external="{53B582F1-ECEB-4581-BC06-19C24228B11F}"
id_graphwork_external="{3A1E523D-3936-4E20-A08A-D07ABAE5BC20}" id_subdiv_internal="1333419"
id_appoint_internal="1333397" id_graphwork_internal="1333495" date_dismiss="" path_photo="">
<identifiers/>
</staffnode>
<staffnode last_name="Богаткина" first_name="Светлана" middle_name="Владимировна" id_internal="1333772"
id_external="" tabel_id="0000000000" date_begin="01.01.1980" date_action="01.01.1980"
id_subdiv_external="" id_appoint_external="{53B582F1-ECEB-4581-BC06-19C24228B11F}"
id_graphwork_external="{3A1E523D-3936-4E20-A08A-D07ABAE5BC20}" id_subdiv_internal="1333411"
id_appoint_internal="1333397" id_graphwork_internal="1333495" date_dismiss="" path_photo="">
<identifiers>
<identifier type_identifier="family_number" id_card="7466" identifier="9236095"
identifier_transformed="9236095" date_begin_action="23.08.2016" date_end_action="01.01.3015"
prohibit="false"/>
</identifiers>
</staffnode>
<staffnode last_name="Борзунова" first_name="Лилия" middle_name="Владимировна" id_internal="1333773"
id_external="" tabel_id="0000000000" date_begin="01.01.1980" date_action="01.01.1980"
id_subdiv_external="" id_appoint_external="{14FBA985-0576-485A-8ED8-D6E81DC92BFC}"
id_graphwork_external="{3A1E523D-3936-4E20-A08A-D07ABAE5BC20}" id_subdiv_internal="1333429"
id_appoint_internal="1333389" id_graphwork_internal="1333495" date_dismiss="" path_photo="">
<identifiers/>
</staffnode>
<staffnode last_name="Бут" first_name="Наталья" middle_name="Николаевна" id_internal="1333775" id_external=""
tabel_id="0000000000" date_begin="01.01.1980" date_action="01.01.1980" id_subdiv_external=""
id_appoint_external="{53B582F1-ECEB-4581-BC06-19C24228B11F}"
id_graphwork_external="{3A1E523D-3936-4E20-A08A-D07ABAE5BC20}" id_subdiv_internal="1333409"
id_appoint_internal="1333397" id_graphwork_internal="1333495" date_dismiss="" path_photo="">
<identifiers/>
</staffnode>
</staff>
</documentrequest>
//parse the response with the list of employees
public static List<Employee> parseXmlStaff(String xml) throws JAXBException {
DocumentRequest documentRequest = (DocumentRequest) parse(xml,
Employee.class, AccessCard.class,DocumentRequest.class);
return documentRequest.getStaff();
}
private static Object parse(String xml, Class...classToBeBound) throws JAXBException {
Reader reader = new StringReader(xml);
JAXBContext context = JAXBContext.newInstance(classToBeBound);
Unmarshaller unmarshaller = context.createUnmarshaller();
return unmarshaller.unmarshal(reader);
}
@XmlRootElement(name = "documentrequest")
public class DocumentRequest {
private List<Employee> staff;
//------------- Getters and Setters ----------------------------
@XmlElement(name = "staffnode")
@XmlElementWrapper(name = "staff")
public List<Employee> getStaff() {
return staff;
}
public void setStaff(List<Employee> staff) {
this.staff = staff;
}
}
@XmlRootElement(name = "staffnode")
public class Employee {
private String firstName;
private String middleName;
private String lastName;
private String pathToPhoto;
private int idInternal;
private String idExternal;
private int tabelId;
private Date dateBegin;
private Date dateAction;
private String idSubdivExternal;
private String idGraphworkExternal;
private String idAppointExternal;
private int idSubdivInternal;
private int idGraphworkInternal;
private int idAppointInternal;
private Date dateDismiss;
private List<AccessCard> accessCards;
public Employee() {
}
public Employee(String firstName, String middleName, String lastName,
String pathToPhoto, int idInternal, String idExternal, int tabelId,
Date dateBegin, Date dateAction, String idSubdivExternal,
String idGraphworkExternal, String idAppointExternal, int idSubdivInternal,
int idGraphworkInternal, int idAppointInternal, Date dateDismiss) {
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
this.pathToPhoto = pathToPhoto;
this.idInternal = idInternal;
this.idExternal = idExternal;
this.tabelId = tabelId;
this.dateBegin = dateBegin;
this.dateAction = dateAction;
this.idSubdivExternal = idSubdivExternal;
this.idGraphworkExternal = idGraphworkExternal;
this.idAppointExternal = idAppointExternal;
this.idSubdivInternal = idSubdivInternal;
this.idGraphworkInternal = idGraphworkInternal;
this.idAppointInternal = idAppointInternal;
this.dateDismiss = dateDismiss;
}
@XmlAttribute(name = "first_name")
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@XmlAttribute(name = "middle_name")
public String getMiddleName() {
return middleName;
}
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
//some code: getters and setters
@XmlAttribute(name = "id_appoint_internal")
public int getIdAppointInternal() {
return idAppointInternal;
}
public void setIdAppointInternal(int idAppointInternal) {
this.idAppointInternal = idAppointInternal;
}
@XmlAttribute(name = "date_dismiss")
@XmlJavaTypeAdapter(DateAdapter.class)
public Date getDateDismiss() {
return dateDismiss;
}
public void setDateDismiss(Date dateDismiss) {
this.dateDismiss = dateDismiss;
}
@XmlElementWrapper(name = "identifiers")
@XmlElement("identifier")
public List<AccessCard> getAccessCard() {
return accessCards;
}
public void setAccessCard(List<AccessCard> accessCards) {
this.accessCards = accessCards;
}
}
@XmlRootElement(name = "identifier")
public class AccessCard {
private String TypeIdentifier;
private int idCard;
private int identifier;
private int identifierTransformed;
private Date dateBegin;
private Date dateEnd;
private boolean prohibit;
@XmlAttribute(name = "type_identifier")
public String getTypeIdentifier() {
return TypeIdentifier;
}
public void setTypeIdentifier(String typeIdentifier) {
TypeIdentifier = typeIdentifier;
}
@XmlAttribute(name = "id_card")
public int getIdCard() {
return idCard;
}
public void setIdCard(int idCard) {
this.idCard = idCard;
}
@XmlAttribute(name = "identifier")
public int getIdentifier() {
return identifier;
}
public void setIdentifier(int identifier) {
this.identifier = identifier;
}
//some code
}
Answer the question
In order to leave comments, you need to log in
How to do it right (IMHO).
- write xsd
- use xsd to generate classes for jaxb using xjc (it's best to do this when building, with a plug-in for maven / gredl)
- use these classes.
Here is an example of a generated class according to your xml line. Everything works for me
https://pastebin.com/JWZJGM8A
Here is main and output when run
import generated.Documentrequest;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.InputStream;
public class Main {
public static void main(String[] args) throws JAXBException {
InputStream xml =
Thread.currentThread().getContextClassLoader().getResourceAsStream("test.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Documentrequest.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Documentrequest documentrequest = (Documentrequest) unmarshaller.unmarshal(xml);
System.out.println(documentrequest.getStaff().getStaffnode().size());
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question