PHP SOAP XML DOM - php

I have a soap xml that contains a bunch of variables that I need to access. Here is the XML.
`<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<searchPersonsResponse xmlns="">
<searchPersonsReturn>
<attributes>
<attributes>
<name>ercreatedate</name>
<values>
<values>201104070130Z</values>
</values>
</attributes>
<attributes>
<name>status</name>
<values>
<values>Stuff1</values>
<values>Stuff2</values>
<values>Stuff3</values>
<values>Stuff4</values>
<values>Stuff5</values>
<values>Stuff6</values>
<values>Stuff7</values>
</values>
</attributes>
</attributes>
<itimDN>blah</itimDN>
<name>Smith, Bob</name>
<profileName>PER</profileName>
<select>false</select>
</searchPersonsReturn>
</searchPersonsResponse>
</soapenv:Body>
</soapenv:Envelope>
I'm trying to access the inner attribute node and pull out the Name and values into a multidimentional array like this ....
$array["status"][0]="stuff1";
$array["status"][1]="stuff2";
$array["status"][2]="stuff3";
$array["status"][3]="stuff4";
so far I have been able to access the nodes but not really get them the way I want. here is the code I have been playing around with .....
$dom_document = new DOMDocument();
$dom_document->loadXML($thexml);
$tag_els_names = $dom_document->getElementsByTagname('name');
$tag_els_values = $dom_document->getElementsByTagname('values');
$data = array();
$data2 = array();
foreach($tag_els_names as $node){
$data[] = array($node->nodeName => $node->nodeValue);
//grabs all the <name> node values
}
$i=0;$j=0;
foreach($tag_els_values as $node){
$j=0;
foreach($node->childNodes as $child) {
$data2[$i][$j] = $child->nodeValue;
//grabs all the value node values
$j++;
}
$i++;
$j=0;
}
Does anyone know an easy way to do this? I think that I have been looking at this for way to long.

How about something like:
$dom_document = new DOMDocument();
$dom_document->loadXML($thexml);
$xpath = new DOMXpath($dom_document);
$attr = $xpath->evaluate("//attribute/attributes");
$names = array();
$values = array();
$i = 0;
foreach($attr as $attr_node) {
$values[i] = array();
foreach($xpath->evaluate("name", $attr_node) as $name){
$names[] = $name->nodeValue;
}
$foreach($xpath->evaluate("value", $attr_node) as $value){
$values[i][] = $value->nodeValue;
}
i++;
}
This would, however, miss the <name> element that's outside of the <attributes> group. Did you mean to be including that?

I figured this out and thought it could help someone else
$doc = new DOMDocument();
$values=array();
if ($doc->loadXML($temp)) {
$attributes = $doc->getElementsByTagName('attributes');
foreach($attributes as $attribute) {
if($attribute->childNodes->length) {
$previous_nodeValue="";
foreach($attribute->childNodes as $i) {
if($i->nodeValue=="status"){
$previous_nodeValue=$i->nodeValue;
}
if($i->nodeName=="values" && $previous_nodeValue== "status"){
foreach($i->childNodes as $j){
$values[]=$j->nodeValue;
}
}
}
$previous_nodeValue="";
}
}
}

Related

create one in xml with php multiple inputs with same name

I have an XML file in which one child has two categories, but with the same name. I want to add one title to each one. How can we do it in PHP?
This is my XML
<root>
<result>
<node>
<title> Some Title Name</title>
<categories>
<category> categor_one </category>
<category> categor_two </category>
</categories>
</node>
<node>
<title> Some Title Name</title>
<categories>
<category> categor_one </category>
<category> categor_tree </category>
</categories>
</node>
</result>
</root>
But I want to obtain this
<root>
<result>
<node>
<title> Some Title Name</title>
<category>categor_one///categor_two </category>
<category1>categor_one///categor_tree</category1>
</node>
</result>
</root>
I managed to impement a function that only gets correctly the category, but if the title is the same it doesn't work as it just creates a new one.
function solve_something($xml, $destination)
{
$xml = simplexml_load_file($xml, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode($xml);
$items = json_decode($json, TRUE);
$products = array();
$product_data = array();
foreach($items['result']['node'] as $item){
$product_data['title'] = $item['title'];
foreach ($item['categories'] as $category) {
if (is_array($category)) {
$product_data['category'] = implode('///', $category);
} else {
$product_data['category'] = $category;
}
}
$products[] = $product_data;
unset($product_data);
}
$path = createXML($products, $destination);
return $path;
}
function createXML($data, $destination)
{
$xmlDoc = new DOMDocument('1.0', 'UTF-8');
$root = $xmlDoc->appendChild($xmlDoc->createElement("root"));
foreach ($data as $key => $product) {
$productA = $root->appendChild($xmlDoc->createElement('product'));
foreach ($product as $key1 => $val) {
if (!empty($val)) {
if ($key1 == 'price' || $key1 == 'tax' || $key1 == 'stockAmount') {
$productA->appendChild($xmlDoc->createElement($key1, $val));
} else {
$ProductKey = $productA->appendChild($xmlDoc->createElement($key1));
$ProductKey->appendChild($xmlDoc->createCDATASection($val));
}
}
}
}
$xmlDoc->formatOutput = true;
fn_rm($destination);
$xmlDoc->save($destination);
return $destination;
}
The output of my code is something like this
<root>
<product>
<title> Some Title Name</title>
<category>categor_one///categor_two </category>
</product>
<product>
<title> Some Title Name</title>
<category>categor_one///categor_tree</category>
</product>
</root>
If you want to keep the data together with the same title, one approach could be to collect that data upfront by using the title as an array key (if it is a valid array key)
When you create the xml, you have the title in the outer foreach loop as the key, and in the inner foreach you can create elements using implode.
Note that in your code you started using product so I took that as a node name.
Example code, which you could use in your code:
$products = array();
$product_data = array();
$xml = simplexml_load_file($xml, "SimpleXMLElement", LIBXML_NOCDATA);
foreach ($xml->result->node as $node) {
$product_data['title'] = (string)$node->title;
foreach($node->categories as $category) {
if (is_array($category)) {
$product_data['category'] = implode('///', $category);
continue;
}
$product_data['category'] = (array)$category->category;
}
$products[(string)$node->title][] = $product_data;
}
$xmlDoc = new DOMDocument('1.0', 'UTF-8');
$root = $xmlDoc->appendChild($xmlDoc->createElement("root"));
foreach ($products as $key => $product) {
$productA = $root->appendChild($xmlDoc->createElement('product'));
$productA->appendChild($xmlDoc->createElement("title", $key));
for ($i = 0; $i < count($product); $i++) {
$productA->appendChild($xmlDoc->createElement("category" . $i, implode("///", $product[$i]["category"])));
}
}
$xmlDoc->formatOutput = true;
echo $xmlDoc->saveXML();
Output
<?xml version="1.0" encoding="UTF-8"?>
<root>
<product>
<title> Some Title Name</title>
<category0> categor_one /// categor_two </category0>
<category1> categor_one /// categor_tree </category1>
</product>
</root>
Php demo

USing php xml generation of specific format

Hi i want to create a php code which generates xml of specific format given below. Kindly help me through this
<?xml version="1.0" encoding="utf-8"?>
<order>
<requisition>
<dateofservice>2012-12-13</dateofservice>
<labid>str1234</labid>
<reqnotes_c>str1234</reqnotes_c>
<rptemail_c>str1234</rptemail_c>
<rptmethod_c>str1234</rptmethod_c>
<specimen_type_c>str1234</specimen_type_c>
<collectiontype_c>str1234</collectiontype_c>
</requisition>
<patient>
<firstname>str1234</firstname>
<lastname>str1234</lastname>
<dob>2012-12-13</dob>
<breed_c>str1234</breed_c>
<species>str1234</species>
<sex>s</sex>
<sterilization_c>str1234</sterilization_c>
</patient>
</order>
My code is this Now correct me please. My code gives only one parent and i want to generate the output as given above
while (($row = fgetcsv($inputFile)) !== FALSE){
$container = $doc->createElement('row');
foreach($headers as $i => $header) {
if(!empty($row[$i])) {
$child = $doc->createElement($header);
$child = $container->appendChild($child);
$value = $doc->createTextNode($row[$i]);
$value = $child->appendChild($value);
}
}
$root->appendChild($container);
}
$strxml = $doc->saveXML();

Deleting an xml node and all children via php

I have been looking around here and google and found various code, but none of them work. I'm assuming it's because none of them match my exact situation. I have an xml structure like this:
<employees>
<employee EmpID="">
<first_name></first_name>
<last_name></last_name>
<ssnum></ssnum>
<status></status>
<contact_info>
<office_phone></office_phone>
<email></email>
<cell_phone></cell_phone>
</contact_info>
<access_info level="user">
<username></username>
<password></password>
</access_info>
<department></department>
<date_started></date_started>
<years></years>
<position></position>
<salary></salary>
<e_increase></e_increase>
<e_raise></e_raise>
<photo></photo>
</employee>
</employees>
I have the EmpID of the employee I want to delete stored in a variable called $ID. I want to remove that employee completely. I have tried things like:
foreach ($doc->getElementsByTagName('employee') as $employee) {
if($employee->getAttribute('EmpID') === $ID) {
foreach ($employee as $node) {
$node->parentNode->removeChild($node);
}
}
}
and other loops trying to get things similarly, as in these posts: PHP XML remove element and all children by name and Remove all children from a XML Node PHP DOM but I have not been able to get this to work successfully.
Any help would be appreciated, thanks.
$ID = '';
$dom = new DOMDocument();
$dom->loadXML($xml_string);
$xpath = new DOMXpath($dom);
$nodes = $xpath->evaluate('/employees/employee[#EmpID = "' . $ID . '"]');
foreach ($nodes as $node) {
$node->parentNode->removeChild($node);
}
there you go:
$xml = '<employees>
<employee EmpID="1">
<first_name></first_name>
<last_name></last_name>
<ssnum></ssnum>
<status></status>
<contact_info>
<office_phone></office_phone>
<email></email>
<cell_phone></cell_phone>
</contact_info>
<access_info level="user">
<username></username>
<password></password>
</access_info>
<department></department>
<date_started></date_started>
<years></years>
<position></position>
<salary></salary>
<e_increase></e_increase>
<e_raise></e_raise>
<photo></photo>
</employee>
<employee EmpID="2">
<first_name></first_name>
<last_name></last_name>
<ssnum></ssnum>
<status></status>
<contact_info>
<office_phone></office_phone>
<email></email>
<cell_phone></cell_phone>
</contact_info>
<access_info level="user">
<username></username>
<password></password>
</access_info>
<department></department>
<date_started></date_started>
<years></years>
<position></position>
<salary></salary>
<e_increase></e_increase>
<e_raise></e_raise>
<photo></photo>
</employee>
</employees>';
$doc = new DOMDocument();
$doc->loadXML($xml);
$selector = new DOMXPath($doc);
$els = $selector->query('/employees//employee');
//or
//$els = $doc->getElementsByTagName('employee');
foreach($els as $el){
if($el->getAttribute('EmpID') == 1){
$el->parentNode->removeChild($el);
}
}
$xml = $doc->saveXML();
echo($xml);
Looking up my xml I have it slightly different .. Give this a try.
foreach ($doc->getElementsByTagName('employee') as $employee) {
if($employee->getAttribute('EmpID') === $ID) {
$employee->parentNode->removeChild($employee);
// $dom->save($xmlFile);
}
}

Manipulating nodes in DomDocument and transform it into an array

I've an XML looking this way :
<?xml version="1.0" ?>
<rss version="2.0">
<channel>
<title>get_news_category</title>
<item>
<id>10502</id>
<title>Cheesecake</title>
<summary>SummaryBlahblah</summary>
</item>
<item>
<id>13236</id>
<title>Moto</title>
<summary>summary blahblah</summary>
</item>
And I want to put the items into an php array.
I've done so far:
$nodes = $dom->getElementsByTagName('item')->item(0);
$values = $nodes->getElementsByTagName("*");
$articles = array();
foreach ($values as $node) {
$articles[$node->nodeName] = $node->nodeValue;
}
var_dump($articles);
Which only returns me in an array, the 1 <item> element. which is logic because i told him ->item(0).
So how to select all the items in order to put all the items into an array ?
Thanks.
use $nodes->length
$dom = new DOMDocument();
$dom->loadHTML($html);
$nodes = $dom->getElementsByTagName('item');
for($i=0; $i<$nodes->length; $i++){
$values = $nodes->item($i)->getElementsByTagName("*");
$articles = array();
foreach ($values as $num => $node) {
$articles[$i][$node->nodeName] = $node->nodeValue;
}
var_dump($articles);
}
You need to iterate the $nodes.
$nodes = $dom->getElementsByTagName('item');
for ($i = 0; $i < $nodes->length; $i++)
{
// Lets grab the node
$values = $nodes->item($i)->getElementsByTagName("*");
}

inserting an object into an array of objects php

I am trying to write a php script which will insert an object into an array of objects which is originally in XML format. I need to insert the object at a specified index and then be able to re-write the xml file from which the data was pulled with the updated object. Here is the structure of my XML
<?xml version="1.0" encoding="UTF-8"?>
<Bars>
<Bar>
<BarName>Kam's</BarName>
<bar_id>0</bar_id>
<Bartenders>
<Bartender>
<fname>Max</fname>
<lname>Vest</lname>
<imageURL>http://uofi-bars.com/bartenderImages/maxvest.jpg</imageURL>
<shift>2</shift>
</Bartender>
</Bartenders>
<Events>
<Event>
<EventName>Kams event</EventName>
<date>08/10/1989</date>
</Event>
</Events>
<Specials>
<Special>Kam's Special 1</Special>
<Special>Kam's Special 2</Special>
</Specials>
</Bar>
So in other words, if I have a bartender who works at a bar with an id of bar_id = 0, I need to be able to insert that bartender into the array of bartenders for that particular bar.
I use the following php code to create the arrays from XML:
function objectsIntoArray($arrObjData, $arrSkipIndices = array())
{
$arrData = array();
// if input is object, convert into array
if (is_object($arrObjData)) {
$arrObjData = get_object_vars($arrObjData);
}
if (is_array($arrObjData)) {
foreach ($arrObjData as $index => $value) {
if (is_object($value) || is_array($value)) {
$value = objectsIntoArray($value, $arrSkipIndices); // recursive call
}
if (in_array($index, $arrSkipIndices)) {
continue;
}
$arrData[$index] = $value;
}
}
return $arrData;
}
$xmlUrl = "Bars.xml"; // XML
$xmlStr = file_get_contents($xmlUrl);
$xmlObj = simplexml_load_string($xmlStr);
$arrXml = objectsIntoArray($xmlObj);
print_r($arrXml);
I guess I just don't know how to refer to this array of objects within an array of objects... Any help would be greatly appreciated!
Thanks!
if you just replace your code:
$xmlUrl = "Bars.xml"; // XML
$xmlStr = file_get_contents($xmlUrl);
$xmlObj = simplexml_load_string($xmlStr);
$arrXml = objectsIntoArray($xmlObj);
print_r($arrXml);
with this:
$xmlUrl = "Bars.xml"; // XML
$xmlStr = file_get_contents($xmlUrl);
$xml = new SimpleXMLElement($xmlStr);
$bartenders = $xml->xpath('//Bartenders');
$new_bartender = $bartenders[0]->addChild('Bartender');
$new_bartender->fname = 'test1';
$new_bartender->lname = 'test2';
$new_bartender->imgURL = 'http://test.com';
$new_bartender->shift = '0';
print_r($bartenders);
this should do the trick, just replace the values with appropriate values :) i hope this helps!!

Categories