XML technologies / SVG / Manipulating graphics with external scripts / Using external scripts for manipulation / Reading XML files with scripts
(Excerpt from “Das Einsteigerseminar SVG — Webgrafiken mit XML” by Helma Spona)
Formatting the charts requires reading the desired formatting and values from the XML file. Open the XML file as an XMLDOM object and read the XML DOM with familiar methods and properties.
The settings files look as follows. Attributes of the parent tag store chart formatting and titles. Child tags store the data series’ values, colors and names.
<?xml version="1.0" encoding="ISO-8859-1"?>
<DIAGRAMM titel="Marktanteile Webbrowser" maxhoehe="110" xAchse="Browser" yAchse="Marktanteil in %" titelx="20" titely="20" titelhoehe="18" diagrammbreite="220" titelcolor="" titelbgcolor="">
<REIHE>
<NAME>Internet Explorer</NAME>
<FARBE>red</FARBE>
<WERT>60</WERT>
</REIHE>
<REIHE>
<NAME>Netscape Navigator</NAME>
<FARBE>orange</FARBE><WERT>20</WERT>
</REIHE>
<REIHE>
<NAME>Opera</NAME>
<FARBE>yellow</FARBE>
<WERT>15</WERT>
</REIHE>
<REIHE>
<NAME>Sonstige</NAME>
<FARBE>darkred</FARBE>
<WERT>5</WERT>
</REIHE>
</DIAGRAMM>
Opening the XML file requires a variable, here ObjXML, assigned an ActiveX object created with new ActiveXObject("microsoft.xmldom"). This produces only an empty object. Fill it using the load method, passing the XML filename. Access the file’s elements again with the method getAttribute to read the parent tag’s attributes.
function INIT(DIAGName)
{
// Einlesen der XML-Datei und Initialisieren der Variablen
var ObjXML=new ActiveXObject("microsoft.xmldom");
var i=0;
var Root;
var Reihe;
var NAME=0;
var FARBE=1;
var WERT=2;
ObjXML.load(DIAGName +".xml");
Root=ObjXML.documentElement;
//Diagrammattribute auslesen
MaxHoehe=Root.getAttribute("maxhoehe");
DiagTitel=Root.getAttribute("titel");
xtext=Root.getAttribute("xAchse");
ytext=Root.getAttribute("yAchse");
xtitel=Root.getAttribute("titelx");
ytitel=Root.getAttribute("titely");
MaxBreite=Root.getAttribute("diagrammbreite");
hoehetitel=Root.getAttribute("titelhoehe");
titelcolor=Root.getAttribute("titelcolor");
titelbgcolor=Root.getAttribute("titelbgcolor");
...
To read each series’ values and colors into arrays, use a loop running from 0 to 3. Access each series with the item method of the ChildNode object, passing its index, and access child elements with the childNodes collection. For readability, the constants WERT, NAME and FARBE are used.
...
//durchlaufen des XML-Dokuments
for (i=0;i<=3;i++)
{
Reihe=Root.childNodes.item(i);
werte[i]=Reihe.childNodes.item(WERT).text;
texte[i]=Reihe.childNodes.item(NAME).text;
farben[i]=Reihe.childNodes.item(FARBE).text;
}
// Formatieren des Diagramms
SVGDoc=document.getElementById(DIAGName).getSVGDocument();
INITDiagramm(SVGDoc);
}
Copyright © verlag moderne industrie GmbH 2001
You may print the online version for your private use.
Otherwise, this chapter from “Das Einsteigerseminar SVG — Webgrafiken mit XML” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
verlag moderne industrie GmbH, Justus-von-Liebig-Straße 1, D-86899 Landsberg/Lech, fon ++49 (0) 81 91 / 125 – 0, leserservice(at)mi-verlag.de
back to top