Page 1 of 1
PHP Simple XML Question
Posted: Tue Oct 14, 2008 2:52 pm
by patwelborn
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:
Posting Code in the Forums to learn how to do it too.
Hello:
I would like to store the <path> value from this XML, then use the variable to save this XML to a file. I'm passing the XML data from Flash to PHP.
This is what the XML looks like:
Code: Select all
<procedure>
<title>Some Title</title>
<path>clients/client0001/Procedures/proc_003.xml</path>
<step>Step</step
</procedure>
This is the PHP code I'm using:
Code: Select all
<?php
$xml = $GLOBALS['HTTP_RAW_POST_DATA'];
$sxe = simplexml_load_string($xml);
if ($sxe === false) {
echo 'Error while parsing the document';
exit;
}
$dom_sxe = dom_import_simplexml($sxe);
if (!$dom_sxe) {
echo 'Error while converting XML';
exit;
}
$dom = new DOMDocument('1.0');
$dom_sxe = $dom->importNode($dom_sxe, true);
$dom_sxe = $dom->appendChild($dom_sxe);
echo $dom->save('test.xml');
?>
The PHP code works as-is, but I cannot figure out how to extract that path value from the XML then use it in my echo save. Any ideas? I've experimented with different Googled solutions for about 3 hours on this. I'm stumped.
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:
Posting Code in the Forums to learn how to do it too.
Re: PHP Simple XML Question
Posted: Tue Oct 14, 2008 2:55 pm
by dude81
Use getElementsByTagName
Re: PHP Simple XML Question
Posted: Tue Oct 14, 2008 3:22 pm
by patwelborn
Thanks dude81. I have tried 3 hours worth of solutions and have not been able to get that to work. Can you provide the specific code that I need to write?
Re: PHP Simple XML Question
Posted: Tue Oct 14, 2008 4:08 pm
by dude81
would you try this
Code: Select all
$dom = new DOMDocument('1.0');
$dom->load('test.xml');
$dom->getElementsByTagName("path");
and let me know you are getting the result
Re: PHP Simple XML Question
Posted: Tue Oct 14, 2008 6:57 pm
by patwelborn
dude81 wrote:would you try this
Code: Select all
$dom = new DOMDocument('1.0');
$dom->load('test.xml');
$dom->getElementsByTagName("path");
and let me know you are getting the result
This does not allow me to access the node value. However, your suggestion combined with another resource yielded this (which works):
Code: Select all
$saveloc = $dom->getElementsByTagName("path");
foreach($blergh as $node) {
echo $node->textContent . " ";
}
However, that foreach loop is unnecessary - there is only one <path>. Any ideas on how to assign that text node value directly to a variable?
Cheers.
Re: PHP Simple XML Question
Posted: Tue Oct 14, 2008 7:14 pm
by dude81
Didn't this work?
Code: Select all
$nodelist = $dom->getElementsByTagName("path");
$nodelist[0]->textContent;
. I dont have dom installed on my system here...
Re: PHP Simple XML Question
Posted: Tue Oct 14, 2008 7:50 pm
by patwelborn
dude81, thanks for leading me down the right path.
Further investigation revealed this useful article:
DOMNODELIST AS AN ARRAY
http://www.compdigitec.com/labs/2008/08 ... -an-array/
Here is the end result of my code. This php script accepts raw xml data from Flash (AS3), converts to a DOM object, extracts a node value containing a save location, then saves to that location. Works.
Code: Select all
<?php
$xml = $GLOBALS['HTTP_RAW_POST_DATA'];
$sxe = simplexml_load_string($xml);
if ($sxe === false) {
echo 'Error while parsing the document';
exit;
}
$dom_sxe = dom_import_simplexml($sxe);
if (!$dom_sxe) {
echo 'Error while converting XML';
exit;
}
$dom = new DOMDocument('1.0');
$dom_sxe = $dom->importNode($dom_sxe, true);
$dom_sxe = $dom->appendChild($dom_sxe);
$nodelist = $dom->getElementsByTagName('path');
echo $dom->save($nodelist->item(0)->nodeValue);
?>
Re: PHP Simple XML Question
Posted: Wed Oct 15, 2008 4:39 pm
by Alpha1980
Please can somebody tell me where I am going wrong?
Code: Select all
<?php // Load and parse the XML document
for ($id = 1; $id <= 500; $id++) {
$allsaints = simplexml_load_file('http://www.allsaintsshop.co.uk/brand-site-scripts/store.php?id='.$id);
echo $allsaints->store->address1;
echo $allsaints->store->address2;
echo $allsaints->store->address3;
echo $allsaints->store->city;
echo $allsaints->store->country;
echo $allsaints->store->postcode;
echo $allsaints->store->telephone;
}
?>
Many thanks.
Re: PHP Simple XML Question
Posted: Wed Oct 15, 2008 5:13 pm
by requinix
SimpleXML, right?
Oh, and if I go to
http://www.allsaintsshop.co.uk/brand-si ... .php?id=23 I get a blank page. No data.
Re: PHP Simple XML Question
Posted: Wed Oct 15, 2008 5:20 pm
by Alpha1980
Try
http://www.allsaintsshop.co.uk/brand-si ... php?id=201
The id's are not necessarily incremental. Could that be the problem?
Re: PHP Simple XML Question
Posted: Wed Oct 15, 2008 5:42 pm
by requinix
Get rid of the ->store part. As the required top-level element it's obvious that you have to navigate through it, so $simplexmlelement->node is the "node" child of that top element.