read Soap Response XML and insert into mysql - php

i'm trying to read response from XML SOAP file and then i need to insert it into the database
here's the XML Code
<SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<MobileAgentAPI:invokeResponse>
<invokeReturn>
<values xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="MobileAgentAPI:SoapMapValue[4]">
<item>
<name>balance</name>
<singleValue>330123</singleValue>
</item>
<item>
<name>returnCode</name>
<singleValue>00</singleValue>
</item>
<item>
<name>transactionStatus</name>
<singleValue>00</singleValue>
</item>
<item>
<name>errorCode</name>
<singleValue>0</singleValue>
</item>
</values>
</invokeReturn>
</MobileAgentAPI:invokeResponse>
</SOAP-ENV:Body>
the question is.
how do i read the response and then insert the data into mysql?
thanks in advance
------UPDATE-------------------
I've tried this approach
$combi = '<XML Response>';
$doc = new DOMDocument();
$doc->loadXML($combi);
echo $doc->getElementsByTagName('name')->item(0)->nodeValue;
and it returns
balance
how do i loop through every node?
and print it like
balance : 330123
returnCode : 00
errorCode : 0
and then insert it into mysql

You can load the XML via DOMDocument in PHP and go through every node
more at: http://php.net/manual/de/class.domdocument.php
if you can specify your question i could help you more
update:
$dom = new DOMDocument;
$dom->loadXML($xml);
$books = $dom->getElementsByTagName('book');
foreach ($books as $book) {
echo $book->nodeValue, PHP_EOL;
}
instead of echo you could implement your sql code for each node
update 2:
$doc->loadXML($combi);
$data = $doc->getElementsByTagName('item');
foreach($data as $data){
echo $data->item(0)->nodeValue . " : " . $data->item(1)->nodeValue, PHP_EOL;
}

Related

PHP - Access Item from XML with xpath()

I want to get the ISBN number of a book from an Amazon XML File. I already checked other posts and tried to solve the problem with them but without any success. The $xml looks like:
<itemlookupresponse>
<items>
<item>
<itemattributes>
<studio>Pottermore from J.K. Rowling</studio>
<eisbn>9781781100769</eisbn>
</itemattributes>
</item>
<item>
<itemattributes>
<studio>Carlsen Verlag GmbH</studio>
<isbn>3551551677</isbn>
</itemattributes>
</item>
<item>
<itemattributes>
<studio>Carlsen</studio>
<isbn>3551551677</isbn>
</itemattributes>
</item>
</items>
</itemlookupresponse>
I want to get the items, where the ISBN equals 3551551677. For that reason, I used the following command, which unfortunately returns an empty array.
$item = $xml->xpath('//items/item[itemattributes/isbn=3551551677]');
I would be glad, if someone could help me and explain, what I made wrong.
XML and XPath are case-sensitive. Amazon's XML is not all lowercase, despite what you've posted in your question. Adjust your XPath to match the exact case used in Amazon's actual XML.
Note also that if there are namespaces involved in the actual XML, those too must be accounted for in your XPath. See How does XPath deal with XML namespaces?
You didn't show something important (namespaces?) because this code works for me:
$string = <<<XML
<itemlookupresponse>
<items>
<item>
<itemattributes>
<studio>Pottermore from J.K. Rowling</studio>
<eisbn>9781781100769</eisbn>
</itemattributes>
</item>
<item>
<itemattributes>
<studio>Carlsen Verlag GmbH</studio>
<isbn>3551551677</isbn>
</itemattributes>
</item>
<item>
<itemattributes>
<studio>Carlsen</studio>
<isbn>3551551677</isbn>
</itemattributes>
</item>
</items>
</itemlookupresponse>
XML;
$xml = new SimpleXMLElement($string);
/* Поиск <a><b><c> */
$items = $xml->xpath('//items/item[ ./itemattributes/isbn[.="3551551677"] ]');
//$items = $xml->xpath('//items/item[itemattributes/isbn=3551551677]');
echo "Result size: " . count($items) . "\n";
foreach ( $items as $item) {
echo $item->asXML() . "\n";
}

Parsing XML with php to Array

I've tried to parse a XML String with php.
The example of XML is
<?xml version="1.0"?>
<root>
<Log>
<Item>
<name>![CDATA[James]]</name>
<address>![CDATA[Korea]]</address>
<email>![CDATA[Korea#gmail.com]]</email>
</Item>
<Item>
<name>![CDATA[James2]]</name>
<address>![CDATA[Korea2]]</address>
<email>![CDATA[Korea#gmail.com2]]</email>
</Item>
</Log>
</root>
My Parsing PHP Code is
$xml = simplexml_load_string($input,null,LIBXML_NOCDATA);
$xml=json_decode( json_encode( $xml),true);
However, When I try to get sizeof($xml['Log']['Item']), have some problems.
If XML have One 'Item' , then sizeof($xml['Log']['Item']) return '3' .
but If XML have 4 'Item's , then it return '4'.
I want to return 1, if XML have One 'Item'.
How can I solve this problem.?
I solved this parsing problem with foreach().
$xml = simplexml_load_string($input,null,LIBXML_NOCDATA);
$itemArray=$xml->Log->Item;
$i=0;
$sqlParamArray= array();
foreach ($itemArray as $tag) {
$name= trim($tag->name);
$address= trim($tag->address);
$email= trim($tag->email);
array_push($sqlParamArray,$name,$address,$email);
$i++;
}
echo $i;
....
sizeof('Item') is saved in $i.
I solved it.

php xml dom extract data from non-standard xml

Hello I have xml like this:
<specs><my>base</my><root>none</root></specs>
<books>
<item>
<id>14</id>
<title>How to live</title>
</item>
<item>
...
</item>
</books>
How can I extract value from < my > ? and then < title >?
when I have data such as :<specs><my>base</my><root>none</root></specs> in xml this code works for me. So how should I modify it to work with data such as books as well in xml?
$dom = new DOMDocument();
$dom->loadXml($xml);
$xpath = new DOMXPath($dom);
$entry = $xpath->query("//xml/specs/my");
foreach($entry as $ent){
echo $ent->nodeValue;
}
simply I added this:
$xml="<xml>".$xml."</xml>";
and now this works $xpath->query("//xml/specs/my"); as well as $xpath->query("//xml/books/item");

Getting a specific part from XML

I am trying to get the PAY where it has the ID 3 Where it says the label phone
but i really dont know how, i tried everything.
Thanks for helping!
Here is the XML code:
$books = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<data>
<login>1</login>
<arrStatsData>
<item>
<Id>500</Id>
<Label>website_name</Label>
<Data>
<item>
<Id>4</Id>
<Label>transactions</Label>
<Data>
<sum>2029.34</sum>
<cst>47.67575</cst>
<num>86</num>
<avg>23.6</avg>
<pay>1981.66</pay>
</Data>
</item>
<item>
<Id>3</Id>
<Label>Phone</Label>
<Data>
<sum>205</sum>
<cst>17.353</cst>
<num>205</num>
<avg>1</avg>
<pay>187.647</pay>
</Data>
</item>
......
PHP Code:
$xml = simplexml_load_string($arrResult); //load xml from above
foreach($xml->arrStatsData->item->Data as $item)
{
foreach($item->item as $DATA)
{
echo $DATA->Id.'<br>';
}
My result now is:
1981.66
187.647
-0.4448
Since you know some information that's tell the node apart from the rest you can use XPath to get the value directly instead of iterating through all of them:
<?php
$sxe = new SimpleXMLElement($books);
$pay = $sxe->xpath('//item[./Id=3]/Data/pay');
echo (string) $pay[0];
Ouput:
187.647
Your PHP code would be like this:
$xml = simplexml_load_string($books);
foreach($xml->arrStatsData->item->Data as $item)
{
//echo '$item;';
foreach($item->item as $DATA)
{
if($DATA->Id == '3'){
echo $DATA->Data->pay."<br/>";
}
}
}
It retrieve the pay value when the ID is equals to 3.
Rolando Isidoro was faster to recommend xpath, my solution is slightly different, that's why I post it, too:
$pay = (string)$xml->xpath("//item[Id = '3']/Data/pay")[0];
echo $pay;
see it working: http://codepad.viper-7.com/qzPlmp

PHP: Find XML node and insert child

I have an xml document with the following structure:
<?xml version="1.0" encoding="UTF-8"?>
<items>
<item>
<id>1</id>
<url>www.test.com</url>
</item>
<item>
<id>2</id>
<url>www.test2.com</url>
</item>
</items>
I would like to be able to search for a node value, such as the value of 1 for the id field. Then, once that node is found, select the parent node, which would be < item > and insert a new child within.
I know the concept of using dom document, but not sure how to do it in this instance.
This should be a start:
$dom = new DOMDocument;
$dom->loadXML($input);
$ids = $dom->getElementsByTagName('id');
foreach ($ids as $id) {
if ($id->nodeValue == '1') {
$child = $dom->createElement('tagname');
$child->appendChild($dom->createTextNode('some text'));
$id->parentNode->appendChild($child);
}
}
$xml = $dom->saveXML();
or something close to it.
You can do the same thing in a simpler way. Instead of looking for an <id/> node whose value is 1 then selecting its parent, you can reverse the relation and look for any node which has an <id/> child whose value is 1.
You can do that very easily in XPath, and here's how to do it in SimpleXML:
$items = simplexml_load_string(
'<?xml version="1.0" encoding="UTF-8"?>
<items>
<item>
<id>1</id>
<url>www.test.com</url>
</item>
<item>
<id>2</id>
<url>www.test2.com</url>
</item>
</items>'
);
$nodes = $items->xpath('*[id = "1"]');
$nodes[0]->addChild('new', 'value');
echo $items->asXML();

Categories