Can I use a WHERE selector (like SQL syntax) on SimpleXML?
Example XML
<?xml version="1.0" encoding="utf-8" ?>
<documentElement>
<row>
<id>1</id>
<name>David</name>
<surname>Johnson</surname>
</row>
<row>
<id>2</id>
<name>Jack</name>
<surname>Nixon</surname>
</row>
</documentElement>
My Example Where Selector
$where = "Jack";
$xml = "example.xml";
$sxml = simplexml_load_string($xml);
foreach ($sxml->row as $data=>$row)
{
if ($where == $data->name) // some code here
else // other some code here
}
Please let me know.
Thank you.
Yes, there is a way: XPath
$where = "Jack";
$xml = "example.xml";
$sxml = simplexml_load_string($xml);
var_dump($sxml->xpath('/documentElement/row/name[.="'.$where.'"]/..'));
No, but you can do this:
$where = "Jack";
$xml = "example.xml";
$sxml = simplexml_load_string($xml);
foreach ($sxml->row as $row)
{
if ($row->name == $where) {
// ...
} else {
// other some code here
}
}
Related
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
I wanted to try to pick up Volume value from the Level1Data node.
Here is the xml:
<Response>
<Content>
<Level1Data Tick="U" Currency="USD" TickSize="0.0001000000" TickValue="0" AssetClass="Equity" InstrumentState="Open" LastPrice="24.1550" LotSize="10"
MinPermittedPrice="0" MaxPermittedPrice="0" ClosePrice="24.0300" OpenPrice="24.1500" FirstPrice="24.1500"
HighPrice="24.7800" LowPrice="24.0000" MaxPrice="24.7800" MinPrice="24.0000" Volume="16238302"
AskSize="105597" BidSize="97618" AskPrice="24.1600" BidPrice="24.1500" Symbol="BAC.NY"
MarketTime="12:08:41.356" Message="L1DB"/>
</Content>
</Response>
And then my main script:
<?php
$result = file_get_contents("lvl1.xml");
// echo $result;
$xml = new SimpleXMLElement($result);
// $dom = new DOMDocument();
// $dom->loadXML("lvl1.xml");
// $vol = dom->getElementsByTagName('Level1Data');
$vol=$xml->children->children('Level1Data');
$id = $xml["Volume"];
echo $id;
?>
Nothing gets returned and I am having a hard time reading the php documentation and their examples.
Thank you.
You can try to find the XML node using attributes() and foreach what attribute you want as per your requirements. If you need only single attribute then discard foreach looping.
<?php
$result =<<<EOT
<Response>
<Content>
<Level1Data Tick="U" Currency="USD" TickSize="0.0001000000" TickValue="0" AssetClass="Equity" InstrumentState="Open" LastPrice="24.1550" LotSize="10"
MinPermittedPrice="0" MaxPermittedPrice="0" ClosePrice="24.0300" OpenPrice="24.1500" FirstPrice="24.1500"
HighPrice="24.7800" LowPrice="24.0000" MaxPrice="24.7800" MinPrice="24.0000" Volume="16238302"
AskSize="105597" BidSize="97618" AskPrice="24.1600" BidPrice="24.1500" Symbol="BAC.NY"
MarketTime="12:08:41.356" Message="L1DB"/>
</Content>
</Response>
EOT;
$volume = '';
$xml = new SimpleXMLElement($result);
foreach($xml->Content->Level1Data[0]->attributes() as $a => $b) {
if($a=='Volume'){
$volume = $b;
}
}
echo $volume;
?>
Demo https://eval.in/839942
OR for single attribute e.g Volume
echo $xml->Content->Level1Data[0]->attributes()->Volume;
If you want to pickup Volume only, it can also be done as follows.
<?php
$result = <<<EOM
<Response>
<Content>
<Level1Data Tick="U" Currency="USD" TickSize="0.0001000000" TickValue="0" AssetClass="Equity" InstrumentState="Open" LastPrice="24.1550" LotSize="10"
MinPermittedPrice="0" MaxPermittedPrice="0" ClosePrice="24.0300" OpenPrice="24.1500" FirstPrice="24.1500"
HighPrice="24.7800" LowPrice="24.0000" MaxPrice="24.7800" MinPrice="24.0000" Volume="16238302"
AskSize="105597" BidSize="97618" AskPrice="24.1600" BidPrice="24.1500" Symbol="BAC.NY"
MarketTime="12:08:41.356" Message="L1DB"/>
</Content>
</Response>
EOM;
$xml = new SimpleXMLElement($result);
echo $xml->Content->Level1Data[0]->attributes()->Volume;
EDIT
<?php
$result = <<<EOM
<Response>
<Content>
<Level1Data Tick="U" Currency="USD" TickSize="0.0001000000" TickValue="0" AssetClass="Equity" InstrumentState="Open" LastPrice="24.1550" LotSize="10"
MinPermittedPrice="0" MaxPermittedPrice="0" ClosePrice="24.0300" OpenPrice="24.1500" FirstPrice="24.1500"
HighPrice="24.7800" LowPrice="24.0000" MaxPrice="24.7800" MinPrice="24.0000" Volume="16238302"
AskSize="105597" BidSize="97618" AskPrice="24.1600" BidPrice="24.1500" Symbol="BAC.NY"
MarketTime="12:08:41.356" Message="L1DB"/>
</Content>
</Response>
EOM;
$xml = new SimpleXMLElement($result);
function recur($obj){
if ( in_array('Level1Data', array_keys( (array) $obj->children()) ) === false){
recur($obj->children());
}else{
var_dump($obj->children()->Level1Data);
exit;
}
}
recur($xml);
I need parsing xml from file to parsing, but i can parsing only local file, how i can parsing file from url?
I use this php code
<?php
include '/example1.php';
$string = $xmlstr;
$xml = simplexml_load_string($string);
foreach($xml->row->exchangerate[0]->attributes() as $a => $b) {
echo $a,'="',$b,"\"\n";
}
?>
example1.php code
<?php
$xmlstr = <<<XML
<exchangerates>
<row>
<exchangerate ccy="RUR" base_ccy="UAH" buy="0.33291" sale="0.33291"/>
</row>
<row>
<exchangerate ccy="EUR" base_ccy="UAH" buy="18.60253" sale="18.60253"/>
</row>
<row>
<exchangerate ccy="USD" base_ccy="UAH" buy="14.97306" sale="14.97306"/>
</row>
</exchangerates>
XML;
?>
Just feed the necessary url and use simplexml_load_file() of you want to use the url:
$url = 'https://api.privatbank.ua/p24api/pubinfo?exchange&coursid=3';
$xml = simplexml_load_file($url);
foreach($xml->row[0]->exchangerate->attributes() as $key => $exchangerate) {
if($key == 'buy' || $key == 'sale') {
$exchangerate = 10 * (float) $exchangerate;
}
echo '<div id="attribute_'.$key.'"></div>','<div id="valuta">'.$exchangerate.'</div><br/>';
}
what about download the xml before loading
<?php
$string = file_get_contents("https://www.unicreditbank.cz/web/exchange_rates_xml.php");
?>
like this
<?php
$string = file_get_contents("https://www.unicreditbank.cz/web/exchange_rates_xml.php");
$xml = simplexml_load_string($string);
foreach($xml->row->exchangerate[0]->attributes() as $a => $b) {
echo $a,'="',$b,"\"\n";
}
?>
I want to delete:
<newWord>
<Heb>צהוב</Heb>
<Eng>yellow</Eng>
</newWord>
from:
<?xml version="1.0" encoding="UTF-8"?>
<xml>
<newWord>
<Heb>מילה ראשונה</Heb>
<Eng>first word</Eng>
</newWord>
<newWord>
<Heb>צהוב</Heb>
<Eng>yellow</Eng>
</newWord>
</xml>
so the output will be:
<?xml version="1.0" encoding="UTF-8"?>
<xml>
<newWord>
<Heb>מילה ראשונה</Heb>
<Eng>first word</Eng>
</newWord>
</xml>
I try to find the tag <newWord> and after this to go to child of it <Eng>yellow</Eng>
and if i found it by $searchString = 'yellow'; I should need to go to parrent of it and delete the element <newWord>.
I try to do it by the following code but I do not know ho to go to child of the <newWord>. many thx for helping.
this my code:
<?php
$del=true;
if ($del==TRUE){
$searchString = 'yellow';
header('Content-type: text/xml; charset=utf-8');
$xml = simplexml_load_file('./Dictionary_user.xml');
foreach($xml->children() as $child){
if($child->getName() == "newWord") {
if($searchString == $child['Eng']) {
$dom->parentNode->removeChild($xml);
} else {
echo('no match found resualt');
}
}
}
$dom = new DOMDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->formatOutput = true;
$dom->load('Dictionary_user.xml');
$dom->save("Dictionary_user.xml");
$dom->saveXML();
header('Location: http://127.0.0.1/www/www1/ajax/ajax4/workwell/popus1.html');
}
?>
Try this:
$searchString = 'yellow';
$xml = simplexml_load_file('./Dictionary_user.xml');
foreach($xml->children() as $child){
if($child->getName() == "newWord") {
if($child->Eng == $searchString){
$dom = dom_import_simplexml($child);
$dom->parentNode->removeChild($dom);
}
}
}
echo $xml->asXML();
On this line
if($searchString == $child['Eng']) {
You're trying to compare the body of the child node, but it doesn't convert to a string automatically. It's still a SimpleXMLElement object, so the comparison fails.
Try explicitly casting it to a string to get the body of the tag.
if($searchString == (string)$child['Eng']) {
The following code:
// Read Attendees Data (For Organisation)
$attendServ = new AttendeeService();
$attendData = $attendServ->getAllActiveAttendeeByOrg($organisation_id);
$attendee = new DOMDocument('1.0');
$attendee->formatOutput = true;
$root = $attendee->createElement('attendee');
$root = $attendee->appendChild($root);
for ($i=0;$i<count($attendData);$i++) {
$row = $attendee->createElement('row');
$row = $root->appendChild($row);
foreach ($attendData[$i] as $tag=>$value)
{
$nodename = $attendee->createElement($tag);
$nodename = $row->appendChild($nodename);
$nodevalue = $attendee->createTextNode($value);
$nodevalue = $row->appendChild($nodevalue);
}
}
// Test Organisation Output
header ("Content-Type:text/xml");
echo $attendee->saveXML();
Produces:
<?xml version="1.0"?>
<attendee>
<row>
<attendee_id/>1
<attendee_name/>A
<attendee_initials/>A.1.
</row>
</attendee>
Instead of:
<?xml version="1.0"?>
<attendee>
<row>
<attendee_id>1</attendee_id>
<attendee_name>A</attendee_name>
<attendee_initials>A.1.</attendee_initials>
</row>
</attendee>
Any clue where I am going wrong?
You are appending the text node to the row:
$nodename = $attendee->createElement($tag);
$nodename = $row->appendChild($nodename);
$nodevalue = $attendee->createTextNode($value);
$nodevalue = $row->appendChild($nodevalue);
You want to append it to the element you just created.
$element = $attendee->createElement($tag);
$textNode = $attendee->createTextNode($value);
$element->appendChild($nodevalue);
$row->appendChild($element);