0

Please help me, I am using the JAXB util, I am trying to convert XML string with namespace ns2 to Java object but when I try with below util method it returns null.

<ns2:Document xmlns:ns2="urn:iso:std:iso:20022:tech:xsd:pain.001.001.05">
    <ns2:book id="1">
        <ns2:title>Book1</ns2:title>
        <ns2:date>2022-09-29T23:44:34.867+02:00</ns2:date>
    </ns2:book>
</ns2:Document>

Book Type

public class Book {
    private String title;
    private Date date;

    //setter, getters and constructur

}

Unmarshal Util

public Book unmarshall() throws JAXBException, IOException {
        JAXBContext context = JAXBContext.newInstance(Book.class);

        String xmlString = "<ns2:Document xmlns:ns2=\"urn:iso:std:iso:20022:tech:xsd:pain.001.001.05\"> <ns2:book id=\"1\"> <ns2:title>Book1</ns2:title> <ns2:date>2022-09-29T23:44:34.867+02:00</ns2:date> </ns2:book> </ns2:Document>";

        Book book2 = JAXB.unmarshal(new StringReader(xmlString), Book.class);
    
        System.out.println(book2.getTitle());

        return  book2;
    }
Fezekile Plaatyi
  • 21
  • 1
  • 1
  • 7

0 Answers0