Extract Data off multiple XML row - php

I have a really weird XML response and i need to extract it's data. I need to get the data in the "value" attribute but i need to choose them according to their "key" attributes.
This is how it looks like
<phone>
2125556666
</phone>
<State>
ny
</State>
<Response>
<data key="Supported" value="Yes"/>
<data key="Host" value="Remote"/>
<data key="WholeProductList">
<data key="Product" value="a-z44"/>
<data key="Product" value="c-k99"/>
<data key="Product" value="e-b089"/>
<data key="Product" value="z-p00"/>
<data key="Product" value="r-333"/>
<data key="Product" value="t-RS232"/>
<data key="Product" value="4-lve"/>
<data key="Product" value="Shutdown"/>
</data>
</Response>
In PHP i currenty have
$xmltmp = new DomDocument;
$xmltmp->loadXml($response);
$phone = $xmlresponse->getElementsByTagName('phone')->item(0)->nodeValue;
$state = $xmlresponse->getElementsByTagName('state')->item(0)->nodeValue;
echo $phone;
echo $state;
This currently outputs both phone number and state. It works fine.
Now i need to know if the "Supported" key's value is Yes or No, and if it's Yes, i need to get all "Products". I'm kinda stuck because i am having a hard time making the foreach statement and then checking the "key" attribute value.
Thanks!

Your XML is invalid. An XML document always needs a single document element node.
Example:
<root>
<phone>2125556666</phone>
<State>ny</State>
<Response>
<data key="Supported" value="Yes"/>
...
</data>
</Response>
</root>
The easiest way to fetch data from a DOM is XPath. In PHP that is provided by the DOMXPath class and part of the ext/dom. DOMXPath::evaluate() allows you to fetch node lists or scalar values from the DOM document.
$dom = new DOMDocument;
$dom->loadXml($xml);
$xpath = new DOMXPath($dom);
$phone = $xpath->evaluate('string(/*/phone)');
$state = $xpath->evaluate('string(/*/State)');
var_dump($phone, $state);
Output:
string(10) "2125556666"
string(2) "ny"
An expression like /*/phone selects all phone element child nodes inside the document element. string(/*/phone) casts the first found node into a string and return that. If no node was found, it will return an empty string.
The XPath expression for the supported status is slightly more complex. Conditions for nodes are provided in []. It is possible to compare the result directly in XPath. The return value will be an boolean.
$supported = $xpath->evaluate('/*/Response/data[#key="Supported"]/#value = "Yes"');
var_dump($supported);
Output:
bool(true)
If the expression returns a node list you can iterate it with foreach().
$nodes = $xpath->evaluate(
'/*/Response/data[#key="WholeProductList"]/data[#key="Product"]/#value'
);
$products = [];
foreach ($nodes as $attributeNode) {
$products[] = $attributeNode->value;
}
var_dump($products);
Output:
array(8) {
[0]=>
string(5) "a-z44"
[1]=>
string(5) "c-k99"
[2]=>
string(6) "e-b089"
[3]=>
string(5) "z-p00"
[4]=>
string(5) "r-333"
[5]=>
string(7) "t-RS232"
[6]=>
string(5) "4-lve"
[7]=>
string(8) "Shutdown"
}

This won't quite work "as is" since I don't know what the actual structure of the XML document is, but in short you map the XML nodes to XPath like //root/node/child_node/#attribute and so on.
It should also have some sanity (not null) type checking in.
$xmltmp = new DomDocument;
$xmltmp->loadXml($response);
$xQuery = new DOMXPath($xmltmp);
//not sure what your root node is so the query path is probably wrong
$supported = $xQuery->query('/Response/data[#key="Supported"]/#value')->value;
You can also replace:
$phone = $xmlresponse->getElementsByTagName('phone')->item(0)->nodeValue;
$state = $xmlresponse->getElementsByTagName('state')->item(0)->nodeValue;
With something like (again - without the full structure of the XML document the path itself is probably not quite right):
$phone = $xQuery->query('/phone')->item(0)->nodeValue;
$state = $xQuery->query('/State')->item(0)->nodeValue;

Related

PHP XML Read Multi Nodes

i have a xml like below. How can parse this? i don't know how i can do this?
OZELLIK and DEGER is diffrent sometimes 5 sometimes 10. Please help me.
<?xml version="1.0" encoding="UTF-8"?>
<ROOT>
<STOKLAR>
<STOK>
<SKU>1234</SKU>
<OZELLIKLER>
<OZELLIK>Ekran Kartı Belleği </OZELLIK>
<DEGER>Paylaşımlı </DEGER>
</OZELLIKLER>
</STOK>
<STOK>
<SKU>1454</SKU>
<OZELLIKLER>
<OZELLIK>İşlemci Üreticisi </OZELLIK>
<DEGER>Intel </DEGER>
<OZELLIK>İşlemci Tipi </OZELLIK>
<DEGER>Intel Core i5 </DEGER>
</OZELLIKLER>
</STOK>
</STOKLAR>
</ROOT>
It isn't that difficult with DOM and Xpath expressions:
$document = new DOMDocument();
$document->loadXML($xml);
$xpath = new DOMXpath($document);
// iterate STOK element nodes
foreach ($xpath->evaluate('/ROOT/STOKLAR/STOK') as $stok) {
// fetch first SKU child element node as string
var_dump($xpath->evaluate('string(SKU)', $stok));
// iterate OZELLIK element nodes in OZELLIKLER
foreach ($xpath->evaluate('OZELLIKLER/OZELLIK', $stok) as $ozellik) {
var_dump(
// content of current OZELLIK
$ozellik->textContent,
// first following sibling element node, if DEGER, as string
$xpath->evaluate('string((./following-sibling::*)[1][self::DEGER])', $ozellik)
);
}
}
Output:
string(4) "1234"
string(22) "Ekran Kartı Belleği "
string(14) "Paylaşımlı "
string(4) "1454"
string(21) "İşlemci Üreticisi "
string(6) "Intel "
string(15) "İşlemci Tipi "
string(14) "Intel Core i5 "
DOMXpath::evaluate() can return a node list or a scalar value depending on the expression. The second argument sets the context node for the expression. Here is an explanation of the last (most complex) expression:
Get the following sibling element nodesfollowing-sibling::*
Limit to the first found node(following-sibling::*)[1]
Filter by node name DEGER(following-sibling::*)[1][self::DEGER]
Return text content of this node, empty string if no node was foundstring((following-sibling::*)[1][self::DEGER])
By default expressions work on the "child" axis. The expression uses two other axes "following-sibling" and "self" to look for the required nodes.

PHP simpleXML get value of different nodes depending on value

I have the following XML code:
<administration>
<notes>
<note>
<id>12312312</id>
<name>Lorem Ipsum</name>
<reference>Target Value - 1</reference>
</note>
<note>
<id>12312365</id>
<name>Lorem Ipsum</name>
<references>
<code>Dolor it se met.</code>
<code>Target Value - 2</code>
</references>
</note>
<note>
<id>12375512</id>
<name>Target Value - 3</name>
<reference>S</reference>
</note>
</notes>
<accounting>
<ledgers>
<ledger>
<debits>
<debit>
<description>Target Value - 4</description>
<amount>5467.32</amount>
</debit>
<debit>
<description>My Debit</description>
<amount>5467.32</amount>
<tags>
<tag>Target Value - 5</tag>
</tags>
</debit>
</debits>
<credits>
<credit>
<title>Target Value - 6</title>
<amount>873.00</amount>
</credit>
<credit>
<description>Target Value - 7</description>
<amount>23454.12</amount>
</credit>
</credits>
</ledger>
</ledgers>
</accounting>
</administration>
I'm trying to get a PHP array which consists of only the values of the nodes which have a value containing this string: "Target Value".
This has to be done on a recursive way, using an XML parser (I'm trying SimpleXML, but I'm new to that).
Up 'till now, I've been trying to use SimpleXmlIterator and foreach- and for-loops to achieve this, but I can't seem to check if a node value contains "Target Value".
Edit: reaching the target nodes by manually referring to them is not what I'm looking for, if I were, there would be no problem
Is there any way to achieve this?
EDIT:
Here is the code of my last try:
function sxiToArray($sxi)
{
$a = array();
for( $sxi->rewind(); $sxi->valid(); $sxi->next() )
{
if(!array_key_exists($sxi->key(), $a))
{
$a[$sxi->key()] = array();
}
if($sxi->hasChildren())
{
if (strpos((string)$sxi->current(), "Target Value"))
$a[$sxi->key()][] = sxiToArray($sxi->current());
}
else
{
if (strpos((string)$sxi->current(), "Target Value"))
$a[$sxi->key()][] = strval($sxi->current());
}
}
return $a;
}
$xmlArray = xml2array('../Document.xml');
print_r($xmlArray);
This gives the following result after running:
Array ( [notes] => Array ( ) [accounting] => Array ( ) )
It does not have to be done in an recursive way. You can use Xpath. Xpath uses location paths as part of an expression. The paths use different axes - one of them is descendant. It "ignores" the nesting. Xpath allows you to use conditions.
Get any element node in the document
//*
That has a text node as an child
//*[./text()]
with the text node containing the string "Target Value"
//*[./text()[contains(., "Target Value")]]
Put together it is a fairly small piece of code:
$administration = new SimpleXMLElement($xml);
$nodes = $administration->xpath('//*[./text()[contains(., "Target Value")]]');
foreach ($nodes as $node) {
var_dump($node->getName(), (string)$node);
}
Output:
string(9) "reference"
string(16) "Target Value - 1"
string(4) "code"
string(16) "Target Value - 2"
string(4) "name"
string(16) "Target Value - 3"
string(11) "description"
string(16) "Target Value - 4"
string(3) "tag"
string(16) "Target Value - 5"
string(5) "title"
string(16) "Target Value - 6"
string(11) "description"
string(16) "Target Value - 7"
And with DOM it would not look much different:
$document = new DOMDocument();
$document->loadXml($xml);
$xpath = new DOMXpath($document);
$nodes = $xpath->evaluate('//*[./text()[contains(., "Target Value")]]');
foreach ($nodes as $node) {
var_dump($node->localName, $node->textContent);
}
Why don't you try str_pos() for "Target value"? I don't exactly know how you iterate through the XML but you could do something like:
if(str_pos($node, "Target value"){
//do whatever
}
That will tell you if any of the nodes at least contain that specific string.

PHP iterating over an XML

How can I convert XML to an array that I can iterate over.
Here is the example of my XML
<user>
<student>yes</student>
<id>1</id>
<name>John</name>
</user>
<user>
<student>yes</student>
<id>1</id>
<name>Billy</name>
</user>
My php looks like this
$tmpTemplates = new XDomDocument();
$tmpTemplates->load('.....myFile.xml');
$xPath = new DomXPath($tmpTemplates);
$query = "//user[student='yes']";
$tmpTemplate = $xPath->query($query);
What I want to be able to do is
foreach($tmpTemplate as $tt){
var_dump($tt->student);
var_dump($tt->id);
var_dump($tt->name);
}
Now I'm only able to print out nodeValue which gives me something like this:
yes
1
John
How can I make it an array or an object so I can apprach each value by its key?
You used the same id for both records in your example, so I changed the id of the first one and added a document element.
<users>
<user>
<student>yes</student>
<id>2</id>
<name>John</name>
</user>
<user>
<student>yes</student>
<id>1</id>
<name>Billy</name>
</user>
</users>
Use DOMXpath:evaluate() to fetch the details, the second argument is the context node for the expression.
$document = new DOMDocument();
$document->loadXml($xmlString);
$xPath = new DomXPath($document);
$students = [];
foreach ($xPath->evaluate("//user[student='yes']") as $student) {
$id = $xPath->evaluate('string(id)', $student);
$students[$id] = [
'id' => $id,
'name' => $xPath->evaluate('string(name)', $student)
];
}
var_dump($students);
Output:
array(2) {
[2]=>
array(2) {
["id"]=>
string(1) "2"
["name"]=>
string(4) "John"
}
[1]=>
array(2) {
["id"]=>
string(1) "1"
["name"]=>
string(5) "Billy"
}
}
The return value depends on the expression. A location path like //user[student='yes'] or name returns a DOMNodeList. But you can cast the node list directly in Xpath. string(name) will return the contents of the first name child node or an empty string.
How can I make it an array or an object so I can apprach each value by its key?
Just for clarification, you've got an object so far, it's the SimpleXMLElement and you're using then $tt variable to access it:
...
var_dump($tt->student);
var_dump($tt->id);
var_dump($tt->name);
...
Now that $tt variable comes from another variable, namely by iterating over it, and the other variable is named $tmpTemplate:
...
foreach ($tmpTemplate as $tt) {
var_dump($tt->student);
...
That variable by the way is an array. So you can use it by using the index (starting at zero) to access each <user> element containing a <student> child-element with the value "student" in document-order (as you formulated the xpath for it):
$tmpTemplate[0] contains the first user SimpleXMLElement.
$tmpTemplate[1] contains the second user SimpleXMLElement.
... and so on and so forth.
I hope this makes this a bit more visible.
Try with this php function
xml_parse_into_struct
sample from php manual
<?php
$simple = "<para><note>simple note</note></para>";
$p = xml_parser_create();
xml_parse_into_struct($p, $simple, $vals, $index);
xml_parser_free($p);
echo "Index array\n";
print_r($index);
echo "\nVals array\n";
print_r($vals);
?>
You can use simplexml_load_file, which convert xml to object.
For instance :
$users = simplexml_load_file("users.xml");
foreach($users as $user) {
...
}

var_dump for DOMNodeList printout unexpected result

I have a xml file I load it as the following:
//$file the file system path of the xml file
function getTopicsList($file){
$doc = new DOMDocument();
$doc->load( $file );
var_dump($doc->getElementsByTagName('topic'));
return $doc->getElementsByTagName('topic');
}
The loaded xml file contents is something like the following:
<?xml version="1.0" encoding="UTF-8"?>
<topics>
<topic>
<title>Title1</title>
<keywords>"Some Keys"</keywords>
</topic>
<topic>
<title>The Title</title>
<keywords>Another Key</keywords>
</topic>
<topic>
<title>A Title</title>
<keywords>Key two</keywords>
</topic>
</topics>
The var_dump() in the above code just printout limited information such as:
object(DOMNodeList)#30 (1) {
["length"]=>
int(3)
}
I expected that it should print at least the properties of that object i.e the xml tags and its values. I tried to use other functions such as print_r() and var_export() but there is no details I want.
No, this is node list. You can iterate it with foreach or access nodes using the item() method.
Node lists are used at different places, getElementsByTagName() is one, another is the $childNodes property. Xpath expressions return node lists, too.
Be aware that the nodes can be not only elements but several node types. Like text, cdata section or attribute.
You can use var_dump() to dump a single node. This works with PHP >= 5.3.11 or >= 5.4.1.
$dom = new DOMDocument();
$dom->loadXML('<foo/>');
var_dump($dom->documentElement);
Output:
object(DOMElement)#2 (18) {
["schemaTypeInfo"]=>
NULL
["tagName"]=>
string(3) "foo"
["textContent"]=>
string(0) ""
["baseURI"]=>
string(1) "/"
["localName"]=>
string(3) "foo"
["prefix"]=>
string(0) ""
["ownerDocument"]=>
...

Xpath for a REST response with Keys

I am trying to use Xpath to find a username value from a REST response. When I use
$data = new simpleXMLElement($response->getBody());
$matches = $data->xpath('//KEY[#name="username"]');
I get
array(1) {
[0]=>
object(SimpleXMLElement)#7 (2) {
["#attributes"]=>
array(1) {
["name"]=>
string(8) "username"
}
["VALUE"]=>
string(5) "guest"
}
}
My question is, what is the Xpath expression to get only the value to display? in this example it is guest, but will change depending on the user.
Any suggestions greatly appreciated.
XML structure below.
<?xml version="1.0" encoding="UTF-8" ?>
<RESPONSE>
<MULTIPLE>
<SINGLE>
<KEY name="id">
<VALUE>1</VALUE>
</KEY>
<KEY name="username">
<VALUE>guest</VALUE>
</KEY>
<KEY name="firstname">
<VALUE>Guest user</VALUE>
</KEY>
Not possible with SimpleXML. The xpath method will always return an Array.
But you can do:
$keys = simplexml_load_string($xml);
echo current($keys->xpath('//KEY[#name="username"]/VALUE'));
demo
Or use DOM. The following will return the string immediately:
$dom = new DOMDocument;
$dom->loadXml($xml);
$xpath = new DOMXPath($dom);
echo $xpath->evaluate('string(//KEY[#name="username"]/VALUE)');
demo
I am not sure if simpleXml can evaluate this expression, but any compliant XPath implementation will:
string(//KEY[#name="username"]/VALUE)
When this expression is evaluated, the result is the string value of the Value element that is a child of the (first in document order) KEY element that has a name attribute, whose string value is "username".
Also, if the structure of the XML document is known, then it may be more efficient (fast) not to use //:
string(/RESPONSE/MULTIPLE/SINGLE/KEY[#name="username"]/VALUE)

Categories