parsing xml with PHP simplexml_load_string - php

so i have this XML: the result of var_dump
string '
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<data>
<entry key="message Type">
<![
CDATA[
urgent
]
]></entry><entry key="message Title"><![
CDATA[
teswt
]
]></entry><entry key="message Body"><![
CDATA[
teasd
]
]></entry><entry key="message Priority"><![
CDATA[
1
]
]></entry></data>
' (length=260)
my code looks like this:
$xml = simplexml_load_string($tablerow['data']);
$inputs = $xml->xpath("/schema/user_input[#type!='hidden']/input|/schema/input");
foreach($inputs as $cur_input) {
$cur_input_name = (string)$cur_input['name'];
$cur_input_value = $task_input_values[$cur_input_name];
$input_name = isset($cur_input['label']) ? (string)$cur_input['label'] : $cur_input['name'];
}
var_dump($tablerow['data']);
$tablerow['data'] is the XML from the DB. any help how to get the message Title value from this??
Thanks!

The XPath expression you are using doesn't even resemble the XML document you posted, so I don't know how you would expect to ever find the values you are looking for. The following will extract "teswt" from your sample XML document:
<?php
$xml = simplexml_load_string($tablerow['data']);
$title = '';
foreach ($xml->entry as $entry) {
if ((string) $entry['key'] == 'message Title') {
$title = (string) $entry;
break;
}
}
echo $title;
?>
Edit: Updated to use the SimpleXML "style" rather than XPath.

Your XML should be in proper way.
Check This
<?php
$xmlString = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<data>
<entry key="message Type">
<![CDATA[urgent]]>
</entry>
<entry key="message Title">
<![CDATA[teswt]]>
</entry>
<entry key="message Body">
<![CDATA[xxxxx]]>
</entry>
<entry key="message Priority">
<![CDATA[1]]>
</entry>
</data>';
$xml = simplexml_load_string($xmlString);
foreach ($xml->entry as $entry)
{
echo $entry['key']."<br/>";
}
?>

Related

Add single element/single tag to XML structure with PHP Dom

I try to add a single element/single tag without a closing tag to an XML structure with PHP Dom to get something like this:
<?xml version="1.0" encoding="UTF-8"?>
<ndxml>
<credentials>
<identity>User</identity>
<password>Password</password>
<language name="default" modifier="de"/>
</credentials>
</ndxml>
Does anybody know which command I can use to get this?
$domDocument = new \DOMDocument('1.0', 'UTF-8');
$rootNode = $domDocument->createElement('ndxml');
$credentials = $domDocument->createElement('credentials');
$credentials->appendChild(new \DOMElement('identity', 'User'));
$credentials->appendChild(new \DOMElement('password', 'Password'));
$language = $domDocument->createElement('language');
$language->setAttribute('name', 'default');
$language->setAttribute('modifier', 'de');
$credentials->appendChild($language);
$rootNode->appendChild($credentials);
$domDocument->appendChild($rootNode);
echo $domDocument->saveXML();
echo "\n\n";
echo $domDocument->saveXML(null, LIBXML_NOEMPTYTAG);
OUTPUT:
<?xml version="1.0" encoding="UTF-8"?>
<ndxml><credentials><identity>User</identity><password>Password</password><language name="default" modifier="de"/></credentials></ndxml>
<?xml version="1.0" encoding="UTF-8"?>
<ndxml><credentials><identity>User</identity><password>Password</password><language name="default" modifier="de"></language></credentials></ndxml>

How do I read a soap response with simplexml_load_string()

I am trying to parse a soap response with simplexml_load_string(). I have my soap client set with trace = 1 and exceptions = 0 and $client->__getLastResponse() gives me this result:
<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV = "http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<MDHeader>
<userid>inspector#prime.com</userid>
<password>prime123456</password>
<batchid>1234</batchid></MDHeader>
<RECORDSET>
<ROW id='0'>
<INSPECTIONS>
<FOLDER_ID>835410936</FOLDER_ID>
<FOLDER_ID>835221706</FOLDER_ID>
<FOLDER_ID>835222299</FOLDER_ID>
</INSPECTIONS>
</ROW>
</RECORDSET>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
I then read the results into $xml:
$xml = "<?xml version='1.0' encoding='UTF-8'?>";
$xml .= urldecode($client->__getLastResponse());
An finally try to echo an element with no luck:
$xml1 = simplexml_load_string($xml,null,null,'http://schemas.xmlsoap.org/soap/envelope/',true);
echo "ELEMENT:" . $xml1->Envelope->Body->MDHeader->userid;
I believe it is the namespace SOAP-ENV that is giving me the issue but I don't know how to resolve it.
You can see my test page at: http://www.primevaluationservices.com/myriad/test.php
I think you don't need this line because it is already in the xml:
$xml = "<?xml version='1.0' encoding='UTF-8'?>";
To get the 'userid', I think you can use the SimpleXMLElement children method:
$source = <<<SOURCE
<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV = "http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<MDHeader>
<userid>inspector#prime.com</userid>
<password>prime123456</password>
<batchid>1234</batchid></MDHeader>
<RECORDSET>
<ROW id='0'>
<INSPECTIONS>
<FOLDER_ID>835410936</FOLDER_ID>
<FOLDER_ID>835221706</FOLDER_ID>
<FOLDER_ID>835222299</FOLDER_ID>
</INSPECTIONS>
</ROW>
</RECORDSET>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
SOURCE;
$xml1 = simplexml_load_string($source);
echo $xml1->children('SOAP-ENV', true)->Body->children('')->MDHeader->userid->__toString();
Or the SimpleXMLElement xpath method:
$elements = $xml1->xpath('//SOAP-ENV:Envelope/SOAP-ENV:Body/MDHeader/userid');
$userid = $elements[0]->__toString();
echo '<br>';
echo $userid;
Will both result in:
inspector#prime.com
Demo

php update node value which has attribute

Hi I need to modify xml using php from exsiting xml content
existing xml
<?xml version="1.0" encoding="utf-8"?>
<Book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" BookId="10010">
<STRING id="Name" >Test1</STRING>
<STRING id="ISBN">102399</STRING>
</Book>
new xml
<?xml version="1.0" encoding="utf-8"?>
<Book BookId="10010" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<STRING id="Name" >XYZ</STRING>
<STRING id="ISBN">7777</STRING>
</Book>
Note: BookID should be place before xmlns attribute
Thank you in advance.
Use SimpleXML:
$xml = simplexml_load_file($yourXMLPath);
//Loop for element...
foreach ($xml->STRING as $string) {
//Update value for <STRING id="Name">...
if($string->attributes()->id == 'Name'){
$xml->STRING = 'XYZ';
}
//Update value for <STRING id="Name">...
if($string->attributes()->id == 'ISBN'){
$xml->STRING = '7777';
}
}
// save the updated document
$xml->asXML('newXML.xml');

How insert PHP array in XML?

Hello to all i have a little problem, i have one array like this
$slike=array('1.jpg','2.jpg')
And another XML that looks like this
<?xml version='1.0' encoding='UTF-8'?>
<settings>
<show_context_menu>yes</show_context_menu>
<hide_buttons_delay>2</hide_buttons_delay>
<thumbs_width>200</thumbs_width>
<horizontal_space_btween_buttons>0</horizontal_space_btween_buttons>
<buttons_margins>0</buttons_margins>
</settings>
How to insert $slike in that XML, that new XML looks like this:
<?xml version='1.0' encoding='UTF-8'?>
<settings>
<show_context_menu>yes</show_context_menu>
<hide_buttons_delay>2</hide_buttons_delay>
<thumbs_width>200</thumbs_width>
<horizontal_space_btween_buttons>0</horizontal_space_btween_buttons>
<buttons_margins>0</buttons_margins>
<image>1.jpg</image>
<image>2.jpg</image>
</settings>
Txanks in advance
No idea into which problem you did run, but it's rather trivial using an XML library, e.g. even with SimpleXML in your case:
$slike = array('1.jpg','2.jpg');
$name = 'image';
$doc = new SimpleXMLElement($xml);
foreach($slike as $file) {
$doc->addChild($name, $file);
}
echo $doc->asXML();
The $xml in this example is the xml string from your question. Output:
<?xml version="1.0" encoding="UTF-8"?>
<settings>
<show_context_menu>yes</show_context_menu>
<hide_buttons_delay>2</hide_buttons_delay>
<thumbs_width>200</thumbs_width>
<horizontal_space_btween_buttons>0</horizontal_space_btween_buttons>
<buttons_margins>0</buttons_margins>
<image>1.jpg</image><image>2.jpg</image></settings>
What about something like this:
header("Content-Type: text/xml; charset=utf-8"); // added this line - that way the browser will render it as XML
$slike = array('1.jpg','2.jpg');
$xml = "<?xml version='1.0' encoding='UTF-8'?>
<settings>
<show_context_menu>yes</show_context_menu>
<hide_buttons_delay>2</hide_buttons_delay>
<thumbs_width>200</thumbs_width>
<horizontal_space_btween_buttons>0</horizontal_space_btween_buttons>
<buttons_margins>0</buttons_margins>";
foreach($slike as $s){
$xml.= "<image>".$s."</image>"; // create a new <image> node for each image in array
}
$xml .= "</settings>";
print_r($xml);
Outputs this:
<?xml version='1.0' encoding='UTF-8'?>
<settings>
<show_context_menu>yes</show_context_menu>
<hide_buttons_delay>2</hide_buttons_delay>
<thumbs_width>200</thumbs_width>
<horizontal_space_btween_buttons>0</horizontal_space_btween_buttons>
<buttons_margins>0</buttons_margins>
<image>1.jpg</image>
<image>2.jpg</image>
</settings>
http://php.net/manual/en/book.simplexml.php
The syntax looks something like this for your example
<aaaa Version="1.0">
<bbb>
<cccc>
<dddd Id="id:pass" />
<eeee name="hearaman" age="24" />
</cccc>
</bbb>
</aaaa>
$xml = new SimpleXMLElement($xmlString);
echo $xml->bbb->cccc->dddd['Id'];
echo $xml->bbb->cccc->eeee['name'];
// or...........
foreach ($xml->bbb->cccc as $element) {
foreach($element as $key => $val) {
echo "{$key}: {$val}";
}
}

SimpleXML PHP - Why DOM functions don't work, cdata trouble

Ive been trying every way possible to create cdata entries in my xml. My latest attempt is as follows. I can't even get passed for the first statement where im creating a new DOMDocument. Any ideas?
<?php
$xml = '
<?xml version="1.0" encoding="ISO-8859-1"?>
<cars>
<make name="Ford">
<model>Mustang</model>
</make>
<make name="Honda">
<model>Accord</model>
</make>
</cars>
';
$dom = new DOMDocument;
$dom->loadXML($xml);
$xml = simplexml_import_dom($dom);
print "working";
?>
You should not have any characters before the XML declaration. Remove the line break at $xml = '.
The neatest solution would be to use heredoc syntax:
$xml = <<<XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<cars>
<make name="Ford">
<model>Mustang</model>
</make>
<make name="Honda">
<model>Accord</model>
</make>
</cars>
XML;
Have a look at: DOMDocument::createCDATASection
$xml = '<?xml version="1.0" encoding="ISO-8859-1"?>
<cars>
<make name="Ford">
<model>Mustang</model>
</make>
<make name="Honda">
<model>Accord</model>
</make>
</cars>
';
$dom = new DOMDocument;
$dom->loadXML($xml);
$cdataNode = $dom->createCDATASection('<&>');
$dom->documentElement->appendChild($cdataNode);
echo $dom->saveXml();
Output:
<?xml version="1.0" encoding="ISO-8859-1"?>
<cars>
<make name="Ford">
<model>Mustang</model>
</make>
<make name="Honda">
<model>Accord</model>
</make>
<![CDATA[<&>]]></cars>

Categories