I want to send a pdf file within a xml message. How do I do that in java? What data type do I use in the schema?
Four steps for converting your PDF to XML.Use the Select tool to mark the content you want to save. Right-click the highlighted text. Choose Export Selection As. Select XML, and slick Save.
Choose File > Open. Locate and select the XML file you want to use. Click Open.
XML files are encoded in plaintext, so you can open them in any text editor and be able to clearly read it. Right-click the XML file and select "Open With." This will display a list of programs to open the file in. Select "Notepad" (Windows) or "TextEdit" (Mac).
PDF is not XML. To generate PDF from XML, use XSLT to convert the XML to XSL:FO, which can then be rendered to PDF by an XSL-FO processor such as Apache FOP, Antenna House, or RenderX.
You can transform the PDF file to Base64 Binary and wrap this into a container Element with type xs:base64Binary
. For example you could use this schema definition to place your PDF file in the xml message.
<xs:complexType name="documentType">
<xs:sequence>
<xs:element minOccurs="0" name="mimetype" type="xs:string" />
<xs:element minOccurs="0" name="filename" type="xs:string" />
<xs:element name="content" type="xs:base64Binary" />
</xs:sequence>
</xs:complexType>
You can use org.apache.commons.codec.binary.Base64
for this approach if you already have commons-codec
in your project. It support use of chunked data and strings. For example:
// You can read in the PDF file with FileReader and get the bytes
// Please obey that this solution must be improved for large pdf files
Base64.encodeBase64(binaryData, true)
I suggest you to use bytes array in some tag. For example:
<file>
<name>Test.pdf</name>
<content>here are the bytes of the file</content>
</file>
You can use JAXB to create the xml file automatically from the object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With