Generate Barcode in XML using Java

Generate Barcode in XML using Java

Barcode is a visual representation of data in a machine-readable code. Barcodes contain encoded information about a product or a company in the form of numbers and/or a pattern of parallel lines. Barcode scanners translate the pattern of the bars and extract the encoded information as a simple text. We can generate various types of barcodes programmatically. In this article, we will learn how to generate barcodes in XML using Java.

The following topics shall be covered in this article:

Java Barcode Generator API – Free Download

For generating barcodes in XML, we will be using Aspose.BarCode for Java API. It facilitates generating and reading a wide range of barcode types. Please either download the JAR of the API or just add the following pom.xml configuration in a Maven-based Java application.

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>http://repository.aspose.com/repo/</url>
</repository>
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-barcode</artifactId>
    <version>21.12</version>
</dependency>

How to Generate a Barcode in XML using Java

The BarcodeGenerator class of the API allows barcode generation. We can easily generate a barcode and save it in an XML file using the steps given below:

  1. Firstly, create an instance of the BarcodeGenerator class and specify the barcode’s type and text as an argument.
  2. Finally, generate barcode in XML using BarcodeGenerator.exportToXml(String) method. It takes the path of the output file as an argument.

The following code sample shows how to generate a barcode and save it in XML using Java.

How to Generate a Barcode in XML using Java.
How to Generate a Barcode in XML using Java.

Export Barcode Properties in XML using Java

We can generate a customized barcode and save all the properties in XML using the following steps:

  1. Firstly, create an instance of the BarcodeGenerator class and specify the barcode’s type and text as an argument.
  2. Set various barcode properties such as text, alignment, caption below and caption above settings, etc.
  3. Finally, save barcode in XML using BarcodeGenerator.exportToXml(String) method. It takes the path of the output file as an argument.

The following code sample shows how to export barcode properties in XML using Java.

How to Export a QR Code to XML using Java

We can also generate a QR code and save it in an XML file using the steps given below:

  1. Firstly, create an instance of the BarcodeGenerator class and specify the barcode’s type as QR and text as an argument.
  2. Optionally, set barcode’s features such as height, width, and resolution etc.
  3. Finally, generate QR code in XML using BarcodeGenerator.exportToXml(String) method. It takes the path of the output file as an argument.

The following code sample shows how to generate a QR code and save it in XML using Java.

How to Import a Barcode from XML using Java

We can read barcode properties from an XML file and save the barcode image using the steps given below:

  1. Call BarcodeGenerator.importFromXml() method with input XML file path as argument. It returns the BarcodeGenerator class object.
  2. Finally, save barcode image using BarcodeGenerator.save(String) method. It takes the path of the output file as an argument.

The following code sample shows how to read a barcode from an XML file and save it as an image using Java.

How to Generate a Barcode from XML using Java
How to Generate a Barcode from XML using Java

Get a Free License

Please try the API without evaluation limitations by requesting a free temporary license.

Conclusion

In this article, we have learned how to generate a barcode and export it in XML using Java. We have also seen how to generate a QR code in XML and import a barcode from an XML file programmatically. Besides, you can learn more about Aspose.BarCode for Java API using the documentation. In case of any ambiguity, please feel free to contact us on the forum.

See Also