简体   繁体   中英

Why return null my method with JAXB unmarshalling convert sting xml to object?

I am trying to convert my xml string to objects. I have the following code, but when I do get to a field it returns null.

@Override
public DatosInputDto  convertXmlToDatosInputDto (String xml) {
    DatosInputDto  input = null;
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(DatosInputDto.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        StringReader reader = new StringReader(xml);
        input = (DatosInputDto) unmarshaller.unmarshal(reader);
    } catch (JAXBException ex) {
        LOGGER.error(ex.getMessage(), ex);
    }
    return input;
}

This is the xml of string:

<?xml version="1.0" encoding="UTF-8"?>
<nd0:OU_SchemaPublic xmlns:nd0="http://www.prueba/cadena/ejemplo/1.00">
    <nd0:TTT_Code>LIM28</nd0:TTT_Code>
    <nd0:TTT_Date>20210622</nd0:TTT_Date>
    <nd0:TTT_CodeType>0203</nd0:TTT_CodeType>
    <nd0:TTT_Receiver>0098</nd0:TTT_Receiver>
    <nd0:TTT_SenderPT>0092</nd0:TTT_SenderPT>
    <nd0:TTT_DocumentFile>Documents</nd0:TTT_DocumentFile>
</nd0:OU_SchemaPublic>
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"code", "date", "codeType", "receiver", "senderPt","documentFile"})
@XmlRootElement(name = "OU_SchemaPublic")
public class DatosInputDto implements Serializable {

    /**
    * 
    */
    private static final long serialVersionUID = -4596758570871609534L;

        private String code;
        private String date;
        private String codeType;
        private String receiver
        private String senderPt;
        private String documentFile;
}

package.info

@javax.xml.bind.annotation.XmlSchema(
            xmlns = {@javax.xml.bind.annotation.XmlNs(prefix = "nd0",
                namespaceURI = "http://www.prueba/cadena/ejemplo/1.00")},
            namespace = "http://www.prueba/cadena/ejemplo/1.00",
            elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package com.prueba;

What is my mistake? What do I need to add or remove?

Your XML attributes name different from Pojo class. Add below annotations on fields.

@XmlAttribute
 private String code

If not work then add the key name as below.

@XmlAttribute(name="nd0:TTT_Code")
 private String code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM