I am adjusting a PHP function to write some SQL-statements to an XML file. The XML file must look like:
<condition>
<sequence>
<concat />
<id>patient1</id>
<operator><></operator>
<data>7</data>
</sequence>
<sequence>
<concat />
<id>patient1</id>
<operator><></operator>
<data>7</data>
</sequence>
</condition>
But the PHP code displayed at the bottom of this message generated the following XML:
<condition>
<Sequence />
<Concat />
<Id>patient1</Id>
<Operator>=</Operator>
<Data>0</Data>
<Sequence />
<Concat>AND</Concat>
<Id>patient1</Id>
<Operator>=</Operator>
<Data>22</Data>
<Sequence />
<Concat>AND</Concat>
<Id>patient1</Id>
<Operator>=</Operator>
<Data>44</Data>
<Sequence />
<Concat>AND</Concat>
<Id>patient1</Id>
<Operator>=</Operator>
<Data>45</Data>
</condition>
I am sure the problem is not that difficult for those who worked with DOMDocument before. Seems that you should use the CreateElement method. But As you can see at the structure of the first XML snippet above I want a condition tag, followed by sequence tags with each of them concat, id, operator and data tags. I succeeded in creating a similar structure by using CreateTextNode instead, just for the sequence start en end tag, but when I open the XML file within IE (a bloody shame I know) the CreateTextNode elements are correctly interpreted as values of tags.
$lConditionArr = $this->mSqlStatementObj->getCondition();
if (count($lConditionArr)) {
$lConditionElement = $this->mXMLDOM->createElement("Condition");
$lNewLineElement = $this->mXMLDOM->createTextNode("\n");
$lConditionElement->appendChild($lNewLineElement);
$this->mXMLDOM->documentElement->appendChild($lConditionElement);
foreach ($lConditionArr as $lCondArr) {
//$lTabElement = $this->mXMLDOM->createTextNode("\t");
//$lConditionElement->appendChild($lTabElement);
//$lNewLineElement = $this->mXMLDOM->createTextNode("\n");
//$lConditionElement->appendChild($lNewLineElement);
$lSequenceElement = $this->mXMLDOM->createElement("Sequence");
$lConcatElement = $this->mXMLDOM->createElement("Concat", $lCondArr['con']);
$lTabElement = $this->mXMLDOM->createTextNode("\t");
$lConditionElement->appendChild($lTabElement);
$lConditionElement->appendChild($lSequenceElement);
$lConditionElement->appendChild($lConcatElement);
$lNewLineElement = $this->mXMLDOM->createTextNode("\n");
$lConditionElement->appendChild($lNewLineElement);
$lIdElement = $this->mXMLDOM->createElement("Id", $lCondArr['obj']->getId());
$lTabElement = $this->mXMLDOM->createTextNode("\t");
$lConditionElement->appendChild($lTabElement);
$lConditionElement->appendChild($lIdElement);
$lNewLineElement = $this->mXMLDOM->createTextNode("\n");
$lConditionElement->appendChild($lNewLineElement);
$lOperatorElement = $this->mXMLDOM->createElement("Operator", $lCondArr['opr']);
$lTabElement = $this->mXMLDOM->createTextNode("\t");
$lConditionElement->appendChild($lTabElement);
$lConditionElement->appendChild($lOperatorElement);
$lNewLineElement = $this->mXMLDOM->createTextNode("\n");
$lConditionElement->appendChild($lNewLineElement);
$lDataElement = $this->mXMLDOM->createElement("Data", $lCondArr['dat']);
$lTabElement = $this->mXMLDOM->createTextNode("\t");
$lConditionElement->appendChild($lTabElement);
$lConditionElement->appendChild($lDataElement);
$lNewLineElement = $this->mXMLDOM->createTextNode("\n");
$lConditionElement->appendChild($lNewLineElement);
}
$lNewLineElement = $this->mXMLDOM->createTextNode("\n");
$this->mXMLDOM->documentElement->appendChild($lNewLineElement);
}