Navigation:  Advanced Topics > Reading and writing XML >

Reading XML

Reading XML

Clover has two components for reading in XML.

 

XMLExtract - Reads in an entire XML file and processes it. It allows files of any size to be processed and uses very little memory. I would say that this is the de facto choice for most XML files.
XMLXPathReader - this reads the entire XML into memory (which has obvious ramifications if the XML is too large). Once loaded, you can use XPath expressions to query data that is then passed on to Clover. So if you have relatively small (< 50 MB) files with complex structures and you need to extract a bit of data here and a bit there (rather than bulk processing) then XPath gives you a lot of flexibility.

 

This example is going to concentrate on the reverse of our previous section, "Writing XML" and read back in what we just wrote out. I used the Liquid XML tool (see General Comments) to infer the XSD schema from the CustOrders.xml file that was written. For those of you who want to follow these steps and do not have an XSD inference tool, here is the XSD, which you can save somewhere for use in a moment.

 

<?xml version="1.0" encoding="UTF-8"?>

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="root" type="rootType"/>

<xs:complexType name="CustomerType">

  <xs:sequence>

    <xs:element type="xs:string" name="CustomerID"/>

    <xs:element type="xs:string" name="CompanyName"/>

    <xs:element type="xs:string" name="ContactName"/>

    <xs:element type="xs:string" name="ContactTitle"/>

    <xs:element type="xs:string" name="Address"/>

    <xs:element type="xs:string" name="City"/>

    <xs:element type="xs:string" name="Region" minOccurs="0"/>

    <xs:element type="xs:string" name="PostalCode" minOccurs="0"/>

    <xs:element type="xs:string" name="Country"/>

    <xs:element type="xs:string" name="Phone"/>

    <xs:element type="xs:string" name="Fax" minOccurs="0"/>

    <xs:element type="OrdersType" name="Orders" maxOccurs="unbounded" minOccurs="0"/>

  </xs:sequence>

</xs:complexType>

<xs:complexType name="rootType">

  <xs:sequence>

    <xs:element type="CustomerType" name="Customer" maxOccurs="unbounded" minOccurs="0"/>

  </xs:sequence>

</xs:complexType>

<xs:complexType name="OrdersType">

  <xs:sequence>

    <xs:element type="xs:short" name="OrderID"/>

    <xs:element type="xs:string" name="CustomerID"/>

    <xs:element type="xs:string" name="OrderDate"/>

    <xs:element type="xs:float" name="OrderValue"/>

    <xs:element type="OrderDetailsType" name="OrderDetails" maxOccurs="unbounded" minOccurs="0"/>

  </xs:sequence>

</xs:complexType>

<xs:complexType name="OrderDetailsType">

  <xs:sequence>

    <xs:element type="xs:short" name="OrderID"/>

    <xs:element type="xs:byte" name="ProductID"/>

    <xs:element type="xs:float" name="UnitPrice"/>

    <xs:element type="xs:short" name="Quantity"/>

    <xs:element type="xs:float" name="Discount"/>

  </xs:sequence>

</xs:complexType>

</xs:schema>