How Do I Load an XML document?

Problem

An XML document can be loaded from a string, file, binary data or XML DOM. Each generated object will contain the following methods that allow XML data to be read into the object model. The specifics of this varies depending up the language your using, but the basics methods are described below.

Function Description
FromXml Loads an XML document from a string (See unicode notes)
FromXmlFile Loads an XML document from a file
FromXmlStream Loads an XML document from binary data
FromXmlElement Loads an XML document from a DOM object (Xml Parser object).

Using the Correct Object

You should always load the XML document into the correct type of object.
If you had the following schema.

<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:element name="Person"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="Address"> <xs:complexType> <xs:sequence> <xs:element name="HouseNumber" type="xs:string"/> <xs:element name="PostCode" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>

This would cause 2 classes to be generated Person & Address.
So if you had the XML Data.

<?xml version="1.0" encoding="UTF-8"?> <Person> <name>Joe Blogs</name> <Address> <HouseNumber>15</HouseNumber> <PostCode>LS5 9PQ</PostCode> </Address> </Person>

You would call FromXML on a Person object, as <Person> is the root element in the document
i.e.

byte[] xml = .... Person person = new Person(); person.fromXMLStream(xml);

But what if you don't know what kind of document you want to load until you've looked at it?

In this case you need to open the document first and see, these links show you how.
Loading an Unknown XML Document
Loading an XML Document when its root element is abstract or derived

Function Reference

  C++
Function Description
FromXml

Loads an XML document from a string.
Special care should be taken when loading XML files especially when they contain non-standard chars.
At first glance an XML file looks just like a simple text file, well its not. Its encoded in a specific way typically UTF-8 or UTF-16, and making the assumption that it is just a string can be dangerous especially if you try to convert that string to or from UNICODE. So it is recommended that you treat XML data as a binary resource and use the FromXmlStream method. This can prevent the lost of extended chars. See Unicode Notes

FromXmlFile Loads an XML document from a file
FromXmlStream Loads an XML document from binary data
The data must be held in a CBinaryData object, which can be constructed from a byte array
FromXmlElement Loads an XML document from a DOM object (Xml Parser object).
This allows you to load the document form a DOM, then use the DOM to populate a generated class. This is useful if you need to work out what kind of document you are dealing with up front See loading an unknown document.
  C#
Function Description
FromXml

Loads an XML document from a string.
special care should be taken when loading XML files especially when they contain non-standard chars.
At first glance an XML file looks just like a simple text file, well its not. Its encoded in a specific way typically UTF-8 or UTF-16, and making the assumption that it is just a string can be dangerous especially if you do not apply the correct transform when converting the string (say, to or from UNICODE). So it is recommended that you treat XML data as a binary resource and use the FromXmlStream method. This can prevent the lost of extended chars. See Unicode Notes

FromXmlFile Loads an XML document from a file
FromXmlStream Loads an XML document from binary data
The data must be held in a System.IO.Stream object.
FromXmlElement Loads an XML document from a DOM object.
This allows you to load the document from a DOM (standard .Net class System.Xml.XmlElement), then use the DOM to populate a generated class. This is useful if you need to work out what kind of document you are dealing with up front See loading an unknown document.
  Java
Function Description
fromXml

Loads an XML document from a string.
special care should be taken when loading XML files especially when they contain non-standard chars.
At first glance an XML file looks just like a simple text file, well its not. Its encoded in a specific way typically UTF-8 or UTF-16, and making the assumption that it is just a string can be dangerous especially if you try to convert that string to or from UNICODE. So it is recommended that you treat XML data as a binary resource and use the FromXmlStream method. This can prevent the lost of extended chars. See Unicode Notes

fromXmlFile Loads an XML document from a file
fromXmlStream Loads an XML document from binary data
The data must be held in a byte[].
fromXmlElement Loads an XML document from a DOM object (Xml Parser object).
This allows you to load the document into a DOM, then use the DOM to populate a generated class. This is useful if you need to work out what kind of document you are dealing with up front See loading an unknown document. The DOM parser is the one that comes with Liquid XML, but it can be constructed from a org.w3c.dom.Element
  Visual Basic
Function Description
FromXml

Loads an XML document from a string.
special care should be taken when loading XML files especially when they contain non-standard chars.
At first glance an XML file looks just like a simple text file, well its not. Its encoded in a specific way typically UTF-8 or UTF-16, and making the assumption that it is just a string can be dangerous especially if you try to convert that string to or from UNICODE. So it is recommended that you treat XML data as a binary resource and use the FromXmlStream method. This can prevent the lost of extended chars. See Unicode Notes

FromXmlFile Loads an XML document from a file
FromXmlStream Loads an XML document from binary data
The data must be held in a variant as a byte array.
FromXmlElement Not Implemented

 

Notes

Description Value
Article Created 7/2/2006
Applies to Liquid XML 2005 (4.1) and greater
Also See Unicode Notes
  Loading an Unknown XML Document
  Loading an XML Document when its root element is abstract or derived