title: How to extract XML from XFA PDF document in Java using iText 7 (or other)?
body: <p>Using Java and iText 7, I am trying to exact the XML data from a XFA PDF form in order to parse (and possibly modify) the data but all I can manage to do is grab some basic generic data that is the same for any XFA file I use.</p>
  
  <p>I know it has to be possible since it is done in the iText RUPS tool but I have been going in circles for days now.  </p>
  
  <pre><code>public class Parse {
  
      private PdfDocument pdf;
      private PdfAcroForm form;
      private XfaForm xfa;
      private Document domDocument;
      private Map&lt;Integer, String&gt; data;
      private int numberOfPages;
      private String pdfText;
  
      public void openPdf(String src, String dest) throws IOException, TransformerException {
  
          PdfReader reader = new PdfReader(src);
          reader.setUnethicalReading(true);
          pdf = new PdfDocument(reader, new PdfWriter(dest));
          form = PdfAcroForm.getAcroForm(pdf, true);
  
          data = new HashMap&lt;Integer, String&gt;();
          numberOfPages = getNumberOfPdfPages();
          PdfPage currentPage;
          String textFromPage;
  
          for (int page = 1; page &lt;= numberOfPages; page++) {
              System.out.println("Reading page: " + page + " -----------------");
              currentPage = pdf.getPage(page);
              textFromPage = PdfTextExtractor.getTextFromPage(currentPage);
              data.put(page, textFromPage);
              pdfText += currentPage + ":" + "\n" + textFromPage + "\n";
          }
  
  
          xfa = form.getXfaForm();
          domDocument = xfa.getDomDocument();
          Map&lt;String, Node&gt; map = xfa.extractXFANodes(domDocument);
  
          System.out.println("The template node = " + map.get("template").toString() + "\n");
          System.out.println("Dom document = " + domDocument.toString() + "\n");
          System.out.println("In map form = " + map.toString() + "\n");   
          System.out.println("pdfText = " + pdfText + "\n");
  
          Node node = xfa.getDatasetsNode();
          NodeList list = node.getChildNodes();
  
          for (int i = 0; i &lt; list.getLength(); i++) {
              System.out.println("Get Child Nodes Output = " + list.item(i) + "\n");
          }
  
      }
  }
  </code></pre>
  
  <p>This is the generic output I am receiving.  </p>
  
  <pre><code>Reading page: 1 -----------------
  The template node = [template: null]
  
  Dom document = [#document: null]
  
  In map form = {template=[template: null], form=[form: null], xfdf=[xfdf: null], xmpmeta=[x:xmpmeta: null], datasets=[xfa:datasets: null], config=[config: null], PDFSecurity=[PDFSecurity: null]}
  
  pdfText = nullcom.itextpdf.kernel.pdf.PdfPage@6fa38a:
  
  &gt; Please wait... 
  &gt; 
  &gt; If this message is not eventually replaced by the proper contents of
  &gt; the document, your PDF  viewer may not be able to display this type of
  &gt; document.     You can upgrade to the latest version of Adobe Reader
  &gt; for Windows®, Mac, or Linux® by  visiting 
  &gt; http://www.adobe.com/go/reader_download.     For more assistance with
  &gt; Adobe Reader visit  http://www.adobe.com/go/acrreader.     Windows is
  &gt; either a registered trademark or a trademark of Microsoft Corporation
  &gt; in the United States and/or other countries. Mac is a trademark  of
  &gt; Apple Inc., registered in the United States and other countries. Linux
  &gt; is the registered trademark of Linus Torvalds in the U.S. and other 
  &gt; countries.
  
  Get Child Nodes Output = [xfa:data: null]
  </code></pre>