Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Submit the form XML to a URL over HTTP

Avatar

Level 4

Hi,

I need tips on how to use Submit to URL functionality on the form .

I want the form xml to be submitted to a URL . I understand that there will be a button on the form on which i can specilfy the URL .

But how on the other end i can retrieve that xml and use it ? Can anybody help me with the sample code .

Thanks

Message was edited by: ashish gupta. to make the title more descriptive.

1 Accepted Solution

Avatar

Correct answer by
Former Community Member

If you are unfamiliar with Java this may be a challenge. The servlet below uses the Java IO package to convert the servlet input stream to a string buffer to a string. The DOM4J XML parser is then used to convert the string to an XML document. The servlet writes the XML document to c:\\pdf.xml and sends an HTML response back to the browser (and Preview in Designer which is actually an HTML container) indicating "The XML data was successfully written to c://pdf.xml." A sample pdf.xml is attached.

NOTE: You cannot submit data from the attached PDF in Reader or Acrobat stand-alone. The PDF has to be opened in a browser. Additional scripting is required to submit data from a PDF in Reader or Acrobat stand-alone.

From LiveCycle Designer Preview I can fill the form with data and hit the Submit button resulting a call to the servlet.

Untitled.png

The servlet responds with HTML which is written in the browser container.

Untitled1.png

Untitled2.png

DOM4J is the simplest of the XML parsers for the task. You will need to use a Java IDE such as Eclipse to configure a project to use the Java APIs, the servlet APIs and DOM4J. Alternatively you can use a good old fashioned text editor and a batch file to compile the Java class.

import java.io.IOException;

import java.io.InputStream;

import java.io.FileOutputStream;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.dom4j.Document;

import org.dom4j.DocumentHelper;

import org.dom4j.DocumentException;

import org.dom4j.io.OutputFormat;

import org.dom4j.io.XMLWriter;

public class ReadAndWriteXml extends HttpServlet {

private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

     doPost(req,resp);

}

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

     try {

          InputStream inputStream = req.getInputStream();

          StringBuffer stringBuffer = new StringBuffer();

          byte[] bytes = new byte[4096];

          for (int n; (n = inputStream.read(bytes)) != -1;) {

               stringBuffer.append(new String(bytes, 0, n));

          }

          String xmlStr = new String(stringBuffer.toString());

          String filePath = "C://pdf.xml";

          FileOutputStream fileOutputStream = new FileOutputStream(filePath);

          OutputFormat format = OutputFormat.createPrettyPrint();

          XMLWriter xmlWriter = new XMLWriter(fileOutputStream, format);

          xmlWriter.write(convertString2Document(xmlStr));

          xmlWriter.flush();

          resp.setContentType("text/html");

          PrintWriter out = resp.getWriter();

          out.println("<html><body><center>");

          out.println("<p>The XML data was successfully written to c://pdf.xml.</p>");

          out.println("</center></body></html>");

     }

     catch (Exception e) {

          System.out.println("The following exception occurred: "+ e.getMessage());

     }

}

public Document convertString2Document(String xml) throws DocumentException {

     Document document = null;

     try {

          document = DocumentHelper.parseText(xml);

     }

     catch (DocumentException e) {

          e.printStackTrace();

     }

     return document;

}

}

Steve

View solution in original post

5 Replies

Avatar

Former Community Member

What kind of technology are you running on the server?

If you are Microsoft-centric with an IIS HTTP server you probably should consider an ASP server-side. If you are Java-centric with Apache, Tomcat, JBoss, for example, you should probably consider a JSP (Java server page) or a Java servlet server-side. Or maybe you are running Apache and PHP.

Steve

Avatar

Level 4

I will be using Tomcat with servlet . I need to know what exactly i need to code on the servlet side to get the input xml from the form and use it .

Will the xml be in the adode document format ?

Thanks

Avatar

Correct answer by
Former Community Member

If you are unfamiliar with Java this may be a challenge. The servlet below uses the Java IO package to convert the servlet input stream to a string buffer to a string. The DOM4J XML parser is then used to convert the string to an XML document. The servlet writes the XML document to c:\\pdf.xml and sends an HTML response back to the browser (and Preview in Designer which is actually an HTML container) indicating "The XML data was successfully written to c://pdf.xml." A sample pdf.xml is attached.

NOTE: You cannot submit data from the attached PDF in Reader or Acrobat stand-alone. The PDF has to be opened in a browser. Additional scripting is required to submit data from a PDF in Reader or Acrobat stand-alone.

From LiveCycle Designer Preview I can fill the form with data and hit the Submit button resulting a call to the servlet.

Untitled.png

The servlet responds with HTML which is written in the browser container.

Untitled1.png

Untitled2.png

DOM4J is the simplest of the XML parsers for the task. You will need to use a Java IDE such as Eclipse to configure a project to use the Java APIs, the servlet APIs and DOM4J. Alternatively you can use a good old fashioned text editor and a batch file to compile the Java class.

import java.io.IOException;

import java.io.InputStream;

import java.io.FileOutputStream;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.dom4j.Document;

import org.dom4j.DocumentHelper;

import org.dom4j.DocumentException;

import org.dom4j.io.OutputFormat;

import org.dom4j.io.XMLWriter;

public class ReadAndWriteXml extends HttpServlet {

private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

     doPost(req,resp);

}

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

     try {

          InputStream inputStream = req.getInputStream();

          StringBuffer stringBuffer = new StringBuffer();

          byte[] bytes = new byte[4096];

          for (int n; (n = inputStream.read(bytes)) != -1;) {

               stringBuffer.append(new String(bytes, 0, n));

          }

          String xmlStr = new String(stringBuffer.toString());

          String filePath = "C://pdf.xml";

          FileOutputStream fileOutputStream = new FileOutputStream(filePath);

          OutputFormat format = OutputFormat.createPrettyPrint();

          XMLWriter xmlWriter = new XMLWriter(fileOutputStream, format);

          xmlWriter.write(convertString2Document(xmlStr));

          xmlWriter.flush();

          resp.setContentType("text/html");

          PrintWriter out = resp.getWriter();

          out.println("<html><body><center>");

          out.println("<p>The XML data was successfully written to c://pdf.xml.</p>");

          out.println("</center></body></html>");

     }

     catch (Exception e) {

          System.out.println("The following exception occurred: "+ e.getMessage());

     }

}

public Document convertString2Document(String xml) throws DocumentException {

     Document document = null;

     try {

          document = DocumentHelper.parseText(xml);

     }

     catch (DocumentException e) {

          e.printStackTrace();

     }

     return document;

}

}

Steve

Avatar

Level 4

Thanks a lot Steve . I will use this code  .  Please help me with the javascript code also which i need to write on the submit button , because what i assumed that just giving the servlet URL on the button will solve the purpose . The xml data to be submitted is coming from a stand alone pdf .

Thanks

Avatar

Level 3

HI ,

Can we send the entire PDF?How?I am using dot net at server side.

Thanks ,

Leena

The following has evaluated to null or missing: ==> liqladmin("SELECT id, value FROM metrics WHERE id = 'net_accepted_solutions' and user.id = '${acceptedAnswer.author.id}'").data.items [in template "analytics-container" at line 83, column 41] ---- Tip: It's the step after the last dot that caused this error, not those before it. ---- Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: #assign answerAuthorNetSolutions = li... [in template "analytics-container" at line 83, column 5] ----