McInfo wrote:The most straightforward way to do it would be to open your text editor and type the XML into a new document; but I'm sure you are looking for something a little more dynamic; so you will have to explain the situation in more detail. Where does the data come from? What classes/functions are you using?
Sure thing. I am using the PHP DOM objects rather than simple_xml.
The XML file is built on a customer server, grabbing selected data from their database. The file is then transfered to a new system, and gets imported.
Alas, manually building the file is not an option, this is for a data transfer between servers. The only thing that will change each time is the SELECT query, thus returning different data. The file can be large and hold a lot of data. I am currently testing with approx 20,000 records which get built into the XML file, but can be anywhere up to 700,000 records when in production.
Here is a sample, which works fine at the moment, providing me with a valid, working XML structure.
<root>
<item>
<field1> DATA FROM FIELD 1</field1>
<field2>DATA FROM FIELD 2</field2>
</item>
<item>
<field1> DATA FROM FIELD 1</field1>
<field2>DATA FROM FIELD 2</field2>
</item>
</root>
Code: Select all
$sql = "SELECT field1 AS FIELD1, field2 AS FIELD2 FROM table";
$query = mysql_query($sql)
or die(mysql_error());
// create a new XML document
$doc = new DomDocument('1.0', 'UTF-8');
// create root node
$root = $doc->createElement('ebaytools');
$root = $doc->appendChild($root);
// process one row at a time
while($row = mysql_fetch_assoc($query)) {
// add node for each row
$occ = $doc->createElement('Item');
$occ = $root->appendChild($occ);
// add a child node for each field
foreach ($row as $fieldname => $fieldvalue) {
$child = $doc->createElement($fieldname);
$child = $occ->appendChild($child);
$value = $doc->createTextNode($fieldvalue);
$value = $child->appendChild($value);
}
}
// get completed xml document
$xml_string = $doc->saveXML();
// show me what you built
echo $xml_string;
Now, I want to modify the XML structure, allowing me to capture a second set of data from a new query into the same XML file.
This is what I want to get to:
<ebaytools>
<inventory>
<item>
<name>Sausage</name>
<price>3</price>
</item>
<item>
<name>Sausage</name>
<price>3</price>
</item>
</inventory>
<cats>
<category>
<name>one</name>
<id>2</id>
</category>
<category>
<name>two</name>
<id>4</id>
</category>
</cats>
</ebaytools>
Is this possible? I can't get it to work at all.
Thanks in advance,