Class XmlPrinter

Object
com.zebra.sdk.printer.XmlPrinter

public class XmlPrinter extends Object
A class used to print template formats using XML as input.


package test.zebra.sdk.printer.examples;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 
 import com.zebra.sdk.printer.XmlPrinter;
 
 public class XmlPrinterExample {
 
     public static void main(String[] args) {
 
         xmlPrintingExample();
         cnXmlPrintingExample();
     }
 
     //
     // These examples demonstrate how to use the one-line printing capability of the XmlPrinter class.
     //
     // They assume that a ZPL template containing variable fields appropriate to the XML data
     // specified exists on the host device. In this case, a PC with the file named XmlPrinterExampleTemplate.zpl
     // saved at "c:\XmlPrinterExampleTemplate.zpl". The contents of this file should be...
     //
     // ^XA^DFXmlExamp.zpl^FS
     // ^A0N,100,100^FO100,100^FN1"Name"^FS
     // ^A0N,100,100^FO100,200^FN2"Street"^FS
     // ^A0N,100,100^FO100,300^FN3"City"^FS
     // ^A0N,100,100^FO100,400^FN4"State"^FS
     // ^A0N,100,100^FO100,500^FN5"Zip"^FS
     // ^XZ
     //
 
     private static void xmlPrintingExample() {
         // The possible inputs to the one-line XML printing function(s)
 
         String destinationDevice = "192.168.1.32";
 
         String templateFilename = "c:\\XmlPrinterExampleTemplate.zpl";
         String defaultQuantityString = "1";
         boolean verbose = true;
 
         // If the destination device argument is 'null' then any data that would have been
         // sent to a destination device, had one been specified, is captured in 'outputDataStream'.
         // This provides a way to test your output & configuration without having an actual
         // printer available or without wasting media even if a printer is available.
 
         System.out.println("\nThe destinationDevice connection string argument is null:");
         try {
             ByteArrayOutputStream outputDataStream = new ByteArrayOutputStream();
             XmlPrinter.print(null, getSampleXmlData(), templateFilename, defaultQuantityString, outputDataStream, verbose);
             System.out.println(outputDataStream.toString());
         } catch (Exception e) {
             e.printStackTrace();
         }
 
         // The outputDataStream argument may be null, in which case the data generated by the XmlPrinter class will
         // not be logged but will be sent to the destination device.
         System.out.println("\nThe outputDataStream argument is null:");
         try {
             XmlPrinter.print(destinationDevice, getSampleXmlData(), templateFilename, defaultQuantityString, null, verbose);
         } catch (Exception e) {
             e.printStackTrace();
         }
 
         // Both destinationDevice connection string AND outputDataStream arguments may be specified, in which case the
         // data generated by the XmlPrinter class will be sent to the destination device and logged to the
         // outputDataStream.
         System.out.println("\nBoth destinationDevice connection string and outputDataStream arguments are specified:");
         try {
             ByteArrayOutputStream outputDataStream = new ByteArrayOutputStream();
             XmlPrinter.print(destinationDevice, getSampleXmlData(), templateFilename, defaultQuantityString, outputDataStream, verbose);
             System.out.println(outputDataStream.toString());
         } catch (Exception e) {
             e.printStackTrace();
         }
 
         // At least one of these two (destinationDevice connection string and outputDataStream) arguments should be
         // specified.
     }
 
     private static ByteArrayInputStream getSampleXmlData() {
 
         String sampleXmlData =
                 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
                         + "<file _FORMAT=\"XmlExamp.zpl\">"
                         + " <label>\n"
                         + "     <variable name=\"Name\">John Smith</variable>"
                         + "     <variable name=\"Street\">1234 Anystreet</variable>"
                         + "     <variable name=\"City\">Anycity</variable>"
                         + "     <variable name=\"State\">Anystate</variable>"
                         + "     <variable name=\"Zip\">12345</variable>"
                         + "  </label>\n"
                         + " <label>\n"
                         + "     <variable name=\"Name\">Jane Doe</variable>"
                         + "     <variable name=\"Street\">5678 Anyroad</variable>"
                         + "     <variable name=\"City\">Anytown</variable>"
                         + "     <variable name=\"State\">Anystate</variable>"
                         + "     <variable name=\"Zip\">67890</variable>"
                         + "  </label>\n"
                         + "</file>";
 
         return new ByteArrayInputStream(sampleXmlData.getBytes());
     }
 
     //
     // This example demonstrates how to use the one-line printing capability of the XmlPrinter class.
     //
     // It assume that a ZPL template containing variable fields appropriate to the XML data
     // specified exists on the host device. In this case, a PC with the file named CnXmlPrinterExampleTemplate.zpl
     // saved at "c:\CnXmlPrinterExampleTemplate.zpl". The contents of this file should be...
     //
     // ^XA^DFCnXmlPrinterExampleTemplate.zpl^FS
     // ^A@N,75,75,E:ANMDS.TTF^CI28^FO0,100^FN1"Customer Name"^FS
     // ^A@N,75,75,E:ANMDS.TTF^FO0,200^FN2"Component Name"^FS^
     // ^A@N,75,75,E:ANMDS.TTF^FO0,300^FN3"Vendor Name"^FS
     // ^A@N,75,75,E:ANMDS.TTF^FO0,400^FN4"Vendor ID"^FS
     // ^A@N,75,75,E:ANMDS.TTF^FO0,500^FN5"Invoice Number"^FS
     // ^XZ
 
     private static void cnXmlPrintingExample() {
         // The possible inputs to the one-line XML printing function(s)
 
         String destinationDevice = "192.168.1.32";
 
         String templateFilename = "C:\\CnXmlPrinterExampleTemplate.zpl";
         String defaultQuantityString = "1";
         boolean verbose = true;
 
         // The outputDataStream argument may be null, in which case the data generated by the XmlPrinter class will
         // not be logged but will be sent to the destination device.
         System.out.println("\nThe outputDataStream argument is null:");
         try {
             XmlPrinter.print(destinationDevice, getSampleCnXmlData(), templateFilename, defaultQuantityString, null, verbose);
         } catch (Exception e) {
             e.printStackTrace();
         }
 
     }
 
     private static ByteArrayInputStream getSampleCnXmlData() {
 
         String sampleXmlData =
                 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
                         + "<file _FORMAT=\"XmlExamp.zpl\">"
                         + " <label>\n"
                         + "     <variable name=\"Customer Name\">东风伟世通汽车饰件系统有限公司</variable>"
                         + "     <variable name=\"Component Name\">驾驶员侧仪表板下装饰件</variable>"
                         + "     <variable name=\"Vendor Name\">供应商名称</variable>"
                         + "     <variable name=\"Vendor ID\">供应商代码</variable>"
                         + "     <variable name=\"Invoice Number\">订单号</variable>"
                         + "  </label>\n"
                         + "</file>";
 
         return new ByteArrayInputStream(sampleXmlData.getBytes());
     }
 
 }
 
  • Method Details

    • print

      public static void print(InputStream sourceDataStream, String templateFilename, String defaultQuantityString, OutputStream outputDataStream)
      Print template formats using XML as input data.
      Parameters:
      sourceDataStream - The source stream containing the XML.
      templateFilename - The template to merge the XML to.
      defaultQuantityString - The quantity, if not specified in the data.
      outputDataStream - Optional stream to send data to.
    • print

      public static void print(String destinationDevice, InputStream sourceDataStream, String templateFilename, String defaultQuantityString, OutputStream outputDataStream)
      Print template formats using XML as input data to destinationDevice.
      Parameters:
      destinationDevice - The connection string.
      sourceDataStream - The source stream containing the XML.
      templateFilename - The template to merge the XML to.
      defaultQuantityString - The quantity, if not specified in the data.
      outputDataStream - Optional stream to send data to.
      See ConnectionBuilder for the format of destinationDevice.
    • print

      public static void print(InputStream sourceDataStream, String templateFilename, String defaultQuantityString, OutputStream outputDataStream, boolean verbose)
      Print template formats using XML as input data with optional running commentary to standard out.
      Parameters:
      sourceDataStream - The source stream containing the XML.
      templateFilename - The template to merge the XML to.
      defaultQuantityString - The quantity, if not specified in the data.
      outputDataStream - Optional stream to send data to.
      verbose - If true, print a running commentary to standard out.
    • print

      public static void print(String destinationDevice, InputStream sourceDataStream, String templateFilename, String defaultQuantityString, OutputStream outputDataStream, boolean verbose)
      Print template formats using XML as input data to a device with connection string destinationDevice.
      Parameters:
      destinationDevice - The connection string.
      sourceDataStream - The source stream containing the XML.
      templateFilename - The template to merge the XML to.
      defaultQuantityString - The quantity, if not specified in the data.
      outputDataStream - Optional stream to send data to.
      verbose - If true, print a running commentary to standard out.
      See ConnectionBuilder for the format of destinationDevice.