Programming - XML

XML Introduction

This section is included because, like other application areas, 3D programs are beginning to store their data in XML format. For example X3D which is the new version of VRML. Information about X3D.

There are a lot of advantages in storing 3D data using XML but there are also some issues:

Pros:

  1. XML fits very well with tree structured data.
  2. There are a lot of program tools for working on XML data, also programs and class libraries make it easy to load and store to XML.
  3. XSLT allows XML data to be converted to and from other formats.

Cons:

  1. XML is a text based format which may not store information as compactly as binary data.
  2. Some VRML constructs like DEF/USE and PROTOS are not quite so easily coded in XML.
  3. 3D files tend to contain very large arrays, for example arrays of co-ordinates, these are not very efficiently coded in XML.

Another reason for this section is that we may want to process the program itself, for instance, XSLT might be involved in producing different language versions of the program.


XML

The Extensible Markup Language, abbreviated XML, describes a class of data objects called XML documents and partially describes the behaviour of computer programs which process them. XML is an application profile or restricted form of SGML, the Standard Generalised Markup Language

XML is a means to structure data. Differences between XML and a relational database are:

Elements and Attributes

Each XML document contains one or more elements, the boundaries of which are either delimited by start-tags and end-tags, or, for empty elements, are those of the start-tag. Each element has a type, identified by name, and may have a set of attributes. Each attribute has a name and a value.

The beginning of every non-empty XML element is marked by a start-tag.

For example:

<Transform rotation="0 1 0 1.57" translation="0 -2 1.25">

The Name in the start- and end-tags gives the elements type, in this case Transform.

The end of every element which is not empty is marked by an end-tag containing a name that echoes the element's type as given in the start-tag:

 

An example of the corresponding end-tag to the above start-tag:

</Transform>

 

The text between the start-tag and end-tag is called the element's content. If an element is empty, the start-tag constitutes the whole element. An empty element takes a special form:

For example:

<Material diffuseColor="0.1 0.5 1"/>

Tree structure

<entityName1 attribute1="A" attribute2="B">

<entityName2>

<\entityName1>

so this is a tree structure:

entityName1

+-- entityName2

 


Accessing XML data from a Java Program

More information about Java here.

There are at least 4 ways to serialise Java classes to and from XML.

Simple API for XML (SAX).

An event driven API developed by David Megginson and a number of people on the xml-dev mailing list. This can be much more efficient where you do not need to hold the whole tree structure in memory. SAX support is now distributed with JDK1.4.

Further info about SAX

Document Object Model (DOM).

A tree structure based API issued as a W3C recommendation in October 1998, DOM support is now distributed with JDK1.4. Dom is defined as a set of interfaces This holds the whole tree structure in memory. DOM is useful when your application's central point is to work with an XML document of arbitrary structure, but its generic data structures start to get in the way when you know more about your data than the DOM parser does. The DOM interface is not designed specifically for Object Oriented programs, but for me, the main problem is that the parser generates its own instances of classes to represent elements and attributes, so we cant define our own implementations. This means that we cant

Further info about DOM

Beans

If the Beans interface is implemented, then the classes can be serialised to/from XML using:

There may be downsides to XMLEncoder/Decoder? the documentation seems to suggest that it clones all the data which suggests there may be memory size and performance issues.

Further info about using XMLDecoder/XMLEncoder

JAXB

Given an XML schema, JAXB will generate the necessary interfaces and classes which will allow you to bind those classes to an instance XML document derived
from the schema. You can then treat the XML document as a persistent store for your Java objects. There is an ObjectFactory which you have to use for object creation. You get marshalling, unmarshalling and validation as part of the package.

cover Java & XML, 2nd Edition: Solutions to Real-World Problems

cover Processing XML with Java: A Guide to SAX, DOM, JDOM, JAXP, and TrAX

JDOM

This is a replacement for DOM, not part of SUN distribution

http://jdom.org/

DOM4J

http://dom4j.org/


Accessing XML data from a C# Program

More information about C# here.

The .Net architecture provides class libraries for handling XML in the following namespaces:

System.Xml
System.Xml.Schema
System.Xml.Serialization
System.Xml.Xpath
System.Xml.Xsl

The XmlTextReader class provides similar functionality to SAX

The XmlDocument class implements a DOM interface

cover Beginning C# XML: Essential XML Skills for C# Programmers

cover Professional ASP.NET 1.0 XML with C#

XML Schema Definition Tool

xsd.exe that can be used to convert XSD schemas to C# classes. I tried this on the x3d schema but this gave the following error:

C:\x3d>xsd.exe x3d-3.0.xsd /c
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 1.0.3705.0]
Copyright (C) Microsoft Corporation 1998-2001. All rights reserved.

Schema validation warning: The 'minOccurs' attribute cannot be present. An error occurred at file:///C:/x3d/x3d-3.0.xsd(3192, 4).
Schema validation warning: The 'maxOccurs' attribute cannot be present. An error occurred at file:///C:/x3d/x3d-3.0.xsd(3192, 4).

Warning: Schema could not be validated. Class generation may fail or may produce incorrect results.

Error: Error generating classes for schema 'x3d-3_0'.
- Schema with targetNamespace='' has invalid syntax.
- The 'minOccurs' attribute cannot be present.

If you would like more help, please type "xsd /?".


metadata block
see also:
Correspondence about this page

This site may have errors. Don't use for critical systems.

Copyright (c) 1998-2023 Martin John Baker - All rights reserved - privacy policy.