Questions and Answers - XML Processing



Guidelines, Patterns, and code for end-to-end Java applications.

Questions and Answers - XML Processing
  2. How do I convert the contents from a dynamic XML source into Java objects?

1.   How do I convert the contents from a dynamic XML source into Java objects?

It depends on the type of the dynamic XML source that you have.

  • If your dynamic XML source produces an XHTML page then you should use the JAXP technology (DOM or SAX) to extract the relevant information from the page and assign it to the Java objects. However, this needs a very intimate knowledge of the page layout to separate the actual data from the presentation related information in the page.
  • If your dynamic XML source produces an XML document and that document conforms to a schema (XML schema or DTD) then you have the following options:
    • Use the JAXP technology to programmatically bind the document or part of the document to Java objects
    • Use the JAXB technology to automatically generate classes from the schema which can automatically serialize and deserialize Java objects to and from XML

For example, in the Java Pet Store sample application, we created the PurchaseOrder class and its dependent classes to serialize and deserialize a PurchaseOrder document. We designed these classes to be similar to the classes that the JAXB technology would have generated automatically. This approach is very effective when you have a direct mapping between the schema and the domain object classes.

To isolate the business logic from these details of XML processing, we use helper classes called XML document Editors. These classes are similar to the Data Access Object (DAO) classes but are targeted at editing (creating, assembling, and deassembling) XML documents. These classes are used in several places in the sample application. For example, the supplier application uses the TPASupplierOrderXDE class to validate and transform a TPASupplierOrder document into Java objects. It also uses XSTL to first translate it into a SupplierOrder document conforming to an internal schema. It then uses SAX APIs to extract the OrderId from the incoming document in order to use it as a tracking number. This complex XML processing pipeline is hidden from the business logic using the XDE class since the TPASupplierOrderXDE class only exposes business methods apart from the method to set and get the document.

For more information, read the following document: link


Left Curve
Java SDKs and Tools
Right Curve
Left Curve
Java Resources
Right Curve
Java 8 banner (182)