Generate Barcode in XML using Java

Barcode is a visual representation of data in a machine‑readable code. Barcodes store product or company information as numbers or parallel lines. Barcode scanners read the pattern and return the encoded text. You can generate many barcode types programmatically. In this article, we will learn how to generate barcodes in XML using Java.

The following topics are covered:

Java Barcode Generator API – Free Download

To generate barcodes in XML, we use the Aspose.BarCode for Java API. It supports a wide range of barcode types. Download the JAR or add the following pom.xml configuration to a Maven project.

<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 creates barcodes. Follow these steps:

  1. Create a BarcodeGenerator instance and specify the barcode type and text.
  2. Export the barcode to XML with BarcodeGenerator.exportToXml(String), providing the output file path.

The code sample below demonstrates 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

To export a customized barcode with all its properties:

  1. Create a BarcodeGenerator instance with the desired type and text.
  2. Set properties such as alignment, caption above, caption below, etc.
  3. Call BarcodeGenerator.exportToXml(String) to save the XML file.

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

How to Export a QR Code to XML using Java

You can also generate a QR code and export it to XML:

  1. Instantiate BarcodeGenerator with QR type and the desired text.
  2. Optionally adjust height, width, resolution, and other settings.
  3. Export the QR code to XML using BarcodeGenerator.exportToXml(String).

The code below illustrates how to generate a QR code and save it in XML using Java.

How to Import a Barcode from XML using Java

To read barcode properties from XML and create an image:

  1. Call BarcodeGenerator.importFromXml() with the XML file path. It returns a BarcodeGenerator object.
  2. Save the barcode image using BarcodeGenerator.save(String), providing the output image path.

The sample code demonstrates 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

Try the API without evaluation limits by requesting a free temporary license.

Conclusion

In this article, we learned how to generate a barcode and export it in XML using Java. We also covered how to generate a QR code in XML and how to import a barcode from an XML file programmatically. For more details, see the Aspose.BarCode for Java documentation at the documentation. If you have questions, visit the forum.

See Also