Send pdf file in XML message

I want to send a PDF file in an xml message. how to do it in java? What type of data do I use in the schema?

thanks in advance

+8
java xml pdf
source share
2 answers

You can convert the PDF file to Base64 Binary and wrap it in a container element of type xs:base64Binary . For example, you can use this schema definition to put your PDF file in an 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 supports the use of fragmented data and rows. 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) 
+12
source share

I suggest you use an array of bytes 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 automatically create an xml file from an object.

+1
source share

Source: https://habr.com/ru/post/650401/


All Articles