Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache XmlBeans NullPointerException

I am trying to parse a pacs.003 ISO20022 formatted xml file. I have the XSD for this and using XMLBeans have created the required Java classes. The problem I am having is that I am not able to read an element from the XML and keep getting a NullPointerException. I have searched for similar problems but most result in the OP moving to a different technology.

The XML snippet I have from LON_20160208.xml is:

  <S2SDDDnf:FIToFICstmrDrctDbt xmlns="urn:iso:std:iso:20022:tech:xsd:pacs.003.001.02">
    <GrpHdr>
      <MsgId>DDA160802AASW006543</MsgId>
    </GrpHdr>
  </S2SDDDnf:FIToFICstmrDrctDbt>

My code is:

  public static void main(String[] args) {
    XmlOptions xmlOptions = new XmlOptions();
    xmlOptions.setUseDefaultNamespace();
    xmlOptions.setSavePrettyPrint();

    Document doc;

    try {
        doc = Document.Factory.parse(new File("data_samples/LON_20160208.xml"));
        String messageId = doc.getFIToFICstmrDrctDbt().getGrpHdr().getMsgId();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (XmlException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

The doc.getFIToFICstmrDrctDbt() above results in a NullPointerException and this seems to point to either the get_store() method within the XMLBeans classes or an issue with the namespaces.

I have tried using a substitute namespace map and have toggled the setUseDefaultNamespace() method on and off (it is currently not commented out above). I have also read an answer regarding adding elementFormDefault="qualified" to the xsd:schema element but this has already been done. None of these seem to fix the issue and I am running out of ideas.

Any help would be greatly appreciated.

like image 961
James Fox Avatar asked Aug 02 '16 16:08

James Fox


People also ask

What happened to XMLBeans in Apache POI?

The Apache POI project has unretired the XMLBeans codebase and is maintaining it as a sub-project. Until now the XMLBeans codebase was held in the Apache Attic where former Apache projects are kept for the public good. Some bug fixes (for a more complete list of changes see CHANGES.txt or JIRA).

What is the XMLBeans API?

The XMLBeans API also allows you to reflect into the XML schema itself through an XML Schema Object model. A cursor model through which you can traverse the full XML infoset. Support for XML DOM. For a more complete introduction, see the XMLBeans Overview or Getting Started With XMLBeans .

Why do I have a CVE for XMLBeans?

When parsing XML files using XMLBeans 2.6.0 or below, the underlying parser created by XMLBeans could be susceptible to XML External Entity (XXE) attacks. This issue was fixed a few years ago but on review, we decided we should have a CVE to raise awareness of the issue.

Where is the XMLBeans-<version> jar located?

The xmlbeans-<version>.jar is in the xmlbeans\build directory. Try ant -projecthelp to see other build options. Building on Unix/Linux/Mac or Cygwin is essentially the same as for Windows:


1 Answers

I have been able to fix the issue. It was due to the fact that the message is a more specific SEPA DNF file which is not a generic pacs.003 file. This means it needed a different schema.

The null messages were because the xml does not have a <Document> root tag, it is a <MPEDDDnfBlkDirDeb> root tag.

like image 136
James Fox Avatar answered Sep 22 '22 05:09

James Fox