Source for file XMLImport.php
Documentation is available at XMLImport.php
* Description: hart import data from XML
* (Exported via getExport()->getData()->getXML()->save())
* @copyright (c) 1995-2008 by Steema Software SL. All Rights Reserved. <info@steema.com>
* @link http://www.steema.com
private $sSeriesNode = "";
private $sDataMember = "";
function __get( $property ) {
$method = "get{$property}";
function __set ( $property,$value ) {
$method = "set{$property}";
return $this->$method($value);
/** Load XML from file **/
public function open($file) {
/** Creates a new instance of XMLImport */
private function XMLError($error)/* TODO throws ChartException*/
throw new Exception($error);
* Returns Series (if set) into which to import XML data
return $this->sSeriesNode;
* Set Series into which to import data. If not set Import process will
$this->sSeriesNode = $value;
for($t = 0; $t < sizeof($this->chart->getSeries()); $t++ )
if($this->chart->getSeries($t)->getTitle()->equals($value))
$this->series = $this->chart->getSeries($t);
* Gets ValueList Datamember if set.
return $this->sDataMember;
* Sets ValueList Datamember.
$this->sDataMember = $value;
private function getSelectedNodes($node, $arg)
for($t = 0; $t < sizeof($node->getChildNodes()); $t++ )
if($node->getChildNodes()->item($t)->getNodeName() == $arg)
$tmpNode->appendChild($node->getChildNodes()->item($t));
return $tmpNode->getChildNodes();
private function LoadSeriesNode($nodeDocument) /* TODO throws InstantiationException,
IllegalAccessException, ChartException*/
$tmpSeriesTitle = $nodeDocument->getAttribute("title");
// Create a new Series...
$tmpSeriesName = $nodeDocument->getAttribute("type");
if ($tmpSeriesName != null)
$tmpName = $tmpSeriesName;
if((string) Utils::$seriesTypesOf[$t] == (string) $tmpName)
$tmpClass = Series::newFromType(Utils::$seriesTypesOf[$t]);
$tmpClass->chart = $this->chart;
$idx = $this->chart->addSeries($tmpClass);
$tmpSeries = $this->chart->getSeries($idx);
$tmpTitle = $nodeDocument->getAttribute("title");
$tmpSeries->setTitle($tmpTitle);
$tmpColor = $nodeDocument->getAttribute("color");
$tmpSeries->setColor(Color::fromCode($tmpColor->getNodeValue()));
$tmpPoints = $this->dom->getElementsByTagName("points");
$tmpPoint = $this->dom->getElementsByTagName("point");
$this->XMLError("No <point> nodes.");
$tmpName = $tmpSeries->getMandatory()->valueSource;
$tmpName = $tmpSeries->getMandatory()->getName();
$tmpX = $tmpSeries->getNotMandatory()->valueSource;
$tmpX = $tmpSeries->getNotMandatory()->getName();
for($t = 0; $t < (int) $tmpPoints->item(0)->getAttribute("count"); $t++ )
$node = $tmpPoint->item($t);
$parentNode1 = $node->parentNode;
$parentNode2 = $parentNode1->parentNode;
if($tmpSeriesTitle == $parentNode2->getAttribute("title"))
// If node has attributes - points
if(!$tmpPoint->item($t)->hasAttributes())
$this->XMLError("<point> node has no data.");
$tmpText = $tmpPoint->item($t)->getAttribute("text");
$tmpTex = $tmpText->getNodeValue();
$tmpColor = $tmpPoint->item($t)->getAttribute("color");
$tmpCol = new Color(0,0,0,0,true);
$tmpCol = Color::fromCode($tmpColor);
// Rest of values (if exist)
for($tt = 2; $tt < sizeof($tmpSeries->getValuesLists()); $tt++ )
$tmpList = $tmpSeries->getValuesLists()->getValueList($tt)->valueSource;
$tmpList = $tmpSeries->getValuesLists()->getValueList($tt)->getName();
// TODO remove $tmpValue = $tmpItems->getNamedItem(tmpList);
$tmpValue = $tmpPoint->item($t)->getAttribute($tmpList);
$tmpSeries->getValuesLists()->getValueList($tt)->tempValue = $tmpValue;
$tmpValue = $tmpPoint->item($t)->getAttribute($tmpName);
$tmpValueX = $tmpPoint->item($t)->getAttribute($tmpX);
$tmpSeries->addNull();// .Add(tmpTex);
$tmpSeries->addXY($tmpValueX, 0.0, $tmpTex);
$tmpSeries->addYTextColor($tmpValue, $tmpTex, $tmpCol);
$tmpSeries->addXYTextColor($tmpValueX, $tmpValue, $tmpTex, $tmpCol);
else $this->XMLError("No <points> node.");
* Passes XML Document to be parsed
public function Load($d) /* TODO throws ChartException */
/*create the xPath object _after_ loading the xml source, otherwise the query won't work:*/
$xPath = new DOMXPath($dom);
if(($this->chart != null) || ($this->series != null))
/*nodelist - now get the nodes in a DOMNodeList:*/
//$tmpSeries = $xPath->query($anXPathExpr);
$tmpSeries = $dom->getElementsByTagName("series");
$this->XMLError("No <$this->series> $this->nodes->");
while($this->chart->getSeriesCount() > 0)
$this->chart->removeSeries($this->chart->getSeries(0));
for($t = 0; $t < sizeof($tmpSeries); $t++ )
$this->LoadSeriesNode($tmpSeries->item($t), $d);
$this->XMLError("Empty <$series> $this->node->");
for($t = 0; $t < sizeof($tmpSeries); $t++ )
/*Node*/ $tmpTitle = $tmpSeries->item($t)->getAttribute("title");
$this->LoadSeriesNode($tmpSeries->item($t), $d);
* XML File to process for import
public function open($file)/* TODO throws IOException,
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startTag", "endTag");
xml_set_character_data_handler($xml_parser, "contents");
if (!($fp = fopen($file, "r"))) {
die("could not open XML input");
while ($data = fread($fp, 4096)) { //* review 80000 tambe
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
xml_parser_free($xml_parser);
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory . newInstance();
DocumentBuilder docBuilder = docBuilderFactory . newDocumentBuilder();
Document m_doc = docBuilder . parse(stream);
catch(ChartException cExp)
catch(org . xml . sax . SAXException sar)
catch(javax . xml . parsers . ParserConfigurationException err)
function startTag($parser, $data){
function contents($parser, $data){
function endTag($parser, $data){
|