Saturday, 28 May 2011

Reading about XML and DOM


.xsd   XML Schema Definition
XML
XML is case sensitive and does not ignore white space.
Root
The root element is the only element in an XML document that does not have a parent.
Element: An element node can contain other elements.1) two branches meet. 2) if it is an empty elements, it would be a leaf node.
Attribute: XPath treats attributes as separate nodes from their element hosts. An attribute is like an element that contains only text.
Text
Comment
Processing instruction
Namespace

Makeup and data
The makeup symbols are delineated by angle brackets(<>)

The standard way for searching through XML documents for particular nodes is called XPath,

Parser: The two basic means to parse XML are: tree and stream.
a tree-based parser
Tree style parsing involves loading the entire XML document into memory. The tree file structure allows for random access to the document's elements and for editing of the XML. Examples of tree-type parsing include the DOM and SimpleXML.

DOM (Document Object Model)
1.       The DOM is the W3C DOM specification that you work with in a browser and manipulate with JavaScript. use of the DOM to create an XML string and XML document, formatted for your viewing pleasure.
2.       DOM is a W3C standard, which gives the DOM a lot of authority with developers due to its consistency with other programming languages. Because the DOM builds a tree of the entire document, it uses a lot of memory and processor time.

a SimpleXMLElement object ß> a DOMElement object

SimpleXML
1.    Provided that the XML document isn't too complicated, too deep, and lacks mixed content, SimpleXML is simpler to use than the DOM, as its name implies.
2.    The SimpleXML extension is the tool of choice for parsing an XML document. (easier than DOM)
Extracting multiple instances of an element, access element attributes  
foreach ($xml->book as $book)
To compare an element or attribute with a string or pass it into a function that requires a string, you must cast it to a string using (string). Otherwise, by default, PHP treats the element as an object
easily add children and attributes.
Stream-based parsers: XMLReader and (Simple API for XML) SAX are stream parses

Learning XSLT

*********************************
Some notes on XSLT

XSLT (Doug Tidwell O'Reilly Media, Inc., 2008)
As mention in the book:
It provides a flexible, powerful language for transforming XML documents into something else, such as…, a Scalable Vector Graphics (SVG) file, … So it may be a choice to use XSLT.

The Extensible Stylesheet Language Family (XSL)
XSL is a family of recommendations for defining XML document transformation and presentation.
XSL Transformations (XSLT):a language for transforming XML;
The XML Path Language (XPath):an expression language used by XSLT (and many other languages) to access or refer to parts of an XML document;
XSL Formatting Objects (XSL-FO):an XML vocabulary for specifying formatting semantics.

XSLT (the extensible stylsheet Language for transformations) is a flexible, powerful, yet relatively simple language capable of processing XML


Code Review of Java SVG to KML Converter

Code Review of Java SVG to KML Converter

I found on Inkscape idea page that tbrugz mentioned his SVG to KML convertor in Java. It seems useful to look at.

***********
2010-08-12 - tbrugz
Hi, I´ve developed an initial converter from SVG to KML in Java. It currently converts polygons, given the output "bounds" (north, east, south, west). Infos and hg repository is at https://bitbucket.org/tbrugz/kmlutils/. Feel free to improve on it
***********

*********************************
Several features of this SVG to KML Converter are:
1.    Use the Java XML parser to load SVG
2.    Parse the SVG root and loop through all elements
3.    Only deal with polygons path elements
4.    Apply transformations to SVG polygons to be KML
5.    Save to a KML file

*********************************
Useful for SVG to KML conversion in Inkscape
1.    Need to load and parse SVG, which should exist
2.    Understand the transformations of polygon path coordinates
3.    Write the transformation routine in C++ in Inkscape
4.    Save KML similarly to saving it in Java.


*********************************
Some detail notes on the source code
the algorithm to change coordinates form SVG to KML, seems like:


The author override public void startElement (seems to be called at the begining)
in this function ,call procPolygon, which read in points regarding "L", "M", "Z"
and at the same time call setXYMaxMin(point);(which update inputBounds),
call setPolygonCentre.
 the formula for the coordinates transformation is that:

                        float X = (((p.x - inputBounds[1]) / XinputInterval ) * XoutputInterval ) + outputBounds[1];
                        float Y = (((p.y - inputBounds[3]) / YinputInterval ) * YoutputInterval ) + outputBounds[3];

The following thing is about the inputBounds and outputBounds, and note that
setXYMaxMin(point) will also change the inputBounds:

(In Root.java)

public class Root extends CompositeImpl {

            public float maxX = Float.MIN_VALUE, minX = Float.MAX_VALUE, maxY =
Float.MIN_VALUE, minY = Float.MAX_VALUE;
           
}

related to
float[] inputBounds = {root.maxX, root.minX, root.maxY, root.minY};

and

float[] outputBounds = {outMaxX, outMinX, outMaxY, outMinY,};

where

                        float outMaxX = Float.parseFloat(prop.getProperty("svg2kml.maxX"));
                        float outMinX = Float.parseFloat(prop.getProperty("svg2kml.minX"));
                        float outMaxY = Float.parseFloat(prop.getProperty("svg2kml.maxY"));
                        float outMinY = Float.parseFloat(prop.getProperty("svg2kml.minY"));

(In svg2kml.properties)

svg2kml.maxX=-49.6917
svg2kml.minX=-57.64777

# inverting maxY with minY inverts the vertical orientation of the KML, which is the
desired effect,
# since SVG and KML have opposite vertical orientations
svg2kml.maxY=-33.7515
svg2kml.minY=-27.08