Unable to Access #attributes in XML Node - php

I am trying to access the 'field' element in the 'criteria' node in the following XML:
<?xml version="1.0" encoding="utf-8"?>
<result>
<product>
<data>
<field>spr_tech1</field>
<value>S7</value>
<criteria field="xfield_3">
<criteria_list>Green</criteria_list>
<criteria_list>Beige</criteria_list>
</criteria>
</data>
<data>
<field>spr_tech1</field>
<value>S1</value>
<criteria field="xfield_3">
<criteria_list>Red</criteria_list>
<criteria_list>Blue</criteria_list>
<criteria_list>Yellow</criteria_list>
</criteria>
</data>
<data>
<field>spr_tech1</field>
<value>S7</value>
<criteria field="xfield_3">
<criteria_list>Green</criteria_list>
</criteria>
<criteria field="tech_rt2">
<criteria_list>Transistor</criteria_list>
</criteria>
</data>
</product>
</result>
The code below results in the following error:
Fatal error: Call to a member function attributes() on a non-object
function parseXmlFile($filename)
{
$xml = file_get_contents($filename);
$obj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
$array = json_decode(json_encode($obj), true); // Convert to array}
return $array;
}
$xform = parseXmlFile('transformations.xml');
foreach ($xform['product']['data'] as $data)
{
echo (string)$data['field'] . '<br>';
echo (string)$data['value'] . '<br>';
foreach($data['criteria']->attributes() as $att => $val)
{ echo $att . ' = ' . $val . '<br>'; }
echo $data['criteria']->attributes()->{'field'} . '<br>';
foreach($data['criteria']['criteria_list'] as $att => $val)
{ echo $att . ' = ' . $val . '<br>'; }
echo "-----------------------------------<br>";
}
print "<pre>";
print_r($xform);
print "</pre>";
I've tried a couple methods as you can see in the code, but neither are working for me.
Any help is appreciated!

It seems the problem is when you load the file.
I tried it with file_get_contents() See below
$xml_content = file_get_contents('path to your XML file');
$xform = simplexml_load_string($xml_content);
foreach ($xform['product']['data'] as $data)
{
echo (string)$data['field'] . '<br>';
echo (string)$data['value'] . '<br>';
foreach($data['criteria']->attributes() as $att => $val)
{ echo $att . ' = ' . $val . '<br>'; }
echo $data['criteria']->attributes()->{'field'} . '<br>';
foreach($data['criteria']['criteria_list'] as $att => $val)
{ echo $att . ' = ' . $val . '<br>'; }
echo "-----------------------------------<br>";
}
print "<pre>";
print_r($xform);
print "</pre>";

Your parseXmlFile function can be replaced completely with the built-in simplexml_load_file():
it loads the contents with file_get_contents first; simplexml_load_file vs simplexml_load_string will do this for you
it forces the LIBXML_NO_CDATA option, which is almost certainly not something you need; to get the text contents of a SimpleXML object - including CDATA - you use (string)$node
it converts the incredibly useful SimpleXML object into an array that will be missing half of the data and much worse to work with
To understand SimpleXML, look at the examples in the manual. Elements are accessed with ->name and attributes with ['name'].
So:
$xform = simplexml_load_file('transformations.xml');
foreach ($xform->product->data as $data)
{
echo (string)$data->field . '<br>';
echo (string)$data->value . '<br>';
foreach($data->criteria->attributes() as $att => $val)
{ echo $att . ' = ' . $val . '<br>'; }
echo $data->criteria['field'] . '<br>';
foreach($data->criteria->criteria_list as $number => $node)
{ echo $number . ' = ' . (string)$node . '<br>'; }
echo "-----------------------------------<br>";
}

Related

Getting XML node names without duplicating in simplexml_load_file

I'm getting the node names of an XML using this code:
$url = 'https://www.toptanperpa.com/xml.php?c=shopphp&xmlc=e7ef2a0122';
$xml = simplexml_load_file($url) or die("URL Read Error");
echo $xml->getName() . "<br>";
foreach ($xml->children() as $child) {
echo $child->getName() . "<br>";
foreach ($child->children() as $child2) {
echo $child2->getName() . "<br>";
foreach ($child2->children() as $child3) {
echo $child3->getName() . "<br>";
foreach ($child3->children() as $child4) {
echo $child4->getName() . "<br>";
}
}
}
}
I'm getting the nodes and children correctly, however, it's duplicating.
Result is as below:
urunler
urun
urun_aktif
urun_metaKeywords
urun_metaDescription
urun_url
urun
urun_aktif
urun_metaKeywords
urun_metaDescription
urun_url
Should I just use array_unique or is there a better method?
Thanks
i used recursive function this is simple
function getChildrens($x) {
$children = array();
array_push($children, $x->getName());
foreach ($x->children() as $chld) {
$children = array_merge($children, getChildrens($chld));
}
return array_unique($children);
}
echo implode("<br>", getChildrens($xml));

Parse PHP using simplexml for field values

Trying to parse the following xml:
<?xml version="1.0"?>
<devices>
<device name="TEMP" index="0" available="1" type="" id="xxx">
<field type="2" max="50" min="-20" niceName="Temperature (C)" value="24.75" key="TempC"/>
<field type="2" max="122" min="-4" niceName="Temperature (F)" value="76.55" key="TempF"/>
and pull the TempF value of 76.55.
This is the code I was trying, but I'm not doing something isn't right:
$xml = simplexml_load_file('c:\data.xml') or die("Error: Cannot create object");
echo '<h2>Server Temperature</h2>';
$list = $xml->devices;
for ($i = 0; $i < count($list); $i++) {
echo 'Temp: ' .$list[$i]->TempF[value] . '<br>';
You can loop $devices->device instead without instead of using a for loop
echo '<h2>Server Temperature</h2>';
foreach ($xml->device as $device) {
foreach ($device->field as $field) {
$att = $field->attributes();
if ((string)$att->key === "TempF") {
echo 'Temp: ' . $field->attributes()->value . "<br>";
}
}
}
Output
<h2>Server Temperature</h2>Temp: 76.55<br>
See a Php demo
Or if you want to loop all the values of field, you can use xpath:
echo '<h2>Server Temperature</h2>';
foreach ($xml->xpath('/devices/device/field[#key="TempF"]/#value') as $value) {
echo 'Temp: ' . $value . "<br>";
}
Output
<h2>Server Temperature</h2>Temp: 76.55<br>
See another Php demo

RSS Feed fetch as a JSON object

This is my PHP file named testClient
**public function getXML() {
$xml = simplexml_load_file('https:/.............');
$feed1 = '<h3>' . $xml->channel->title . '</h3>';
foreach ($xml->channel->item as $item) {
$feed1 .= '<h4>' . $item->title . "</h4>";
}
$xml = simplexml_load_file('https://rumble.com/rss.php?target=sprinklevideo');
$feed2 = '<h3>' . $xml->channel->description . '</h3>';
foreach ($xml->channel->item as $item) {
$feed2 .= '<h4>' . $item->description . "</h4>";
}
$xml = simplexml_load_file('https://rumble.com/rss.php?target=sprinklevideo');
$feed3 = '<h3>' . $xml->channel->link . '</h3>';
foreach ($xml->channel->item as $item) {
$feed3 .= '<h4>' . $item->link . "</h4>";
}
return $feed1;
}**
This is my controller class where i want to viewing the result
**
use VideoBundle\VideoProviderClient\testClient as testClient;
class MainController extends Controller
{
public function MainAction() {
$zoo = new testClient();
$val = $zoo->getXML();
//dump
echo "<pre>";
var_dump($val);
echo "</pre>";
die();
return new Response("$val");
}
public function setVal($val) {
$this->item = json_encode($val);
return $this;
}
public function getVal() {
if ($this->item) {
return json_decode($val);
}
return null;
}
**
If i want to fetch data from RSS feed like title/description/link, i am getting those. But HOW CAN I GET ALL THOSE DATA AS A JSON OBJECT. Because in rss feed the data is saving as a item.
This is a sample "item" data from RSS feed ---
**
<item>
<title>Newlyweds stun guests with epic first dance</title>
<description>
your foot in time to the music!
</description>
<link>
https://zzx.com/v2zl27-bride-and-groom-perform-the-best-first-dance-ever-to-a-swingin-classic.html
</link>
<guid isPermaLink="false">media/8.5087</guid>
<pubDate>2015-06-03 12:49:16</pubDate>
<media:category scheme="http://search.yahoo.com/mrss/category_schema">viral</media:category>
<media:keywords>
viral videos, inspiring first wedding dance, songs for first wedding dance
</media:keywords>
<media:thumbnail url="https://rumble.com/rss/8-5019487.jpg" />
<media:player url="https://asdas.com/bin/8_8" height="426" width="491" />
<media:content url="https://i.rmbl.ws/s8/d99" type="video/mp4" />
</item>
**
Thanks in advance ...
You are adding HTML to your data on the first step. You should do that at the end. And the best option would be with a Twig template.
Then parsing XML to a JSON is quite simple:
$xml = simplexml_load_string($xmlstring);
$json = json_encode($xml);

How to parse XML using PHP

I've this code
$xml = simplexml_load_file("http://api.hostip.info/?ip=12.215.42.19");
echo $xml->getName() . "<br />";
foreach($xml->children() as $child) {
echo $child->getName() . ": " . $child . "<br />";
}
If I visit http://api.hostip.info/?ip=12.215.42.19 directly on the browser, I can see the XML return but when I tried the above code, only HostipLookupResultSet<br /> gets echoed.
How do I retrieve the data from this xml? I'm interested in getting coutry and country abbreviation.
I was trying something like echo $xml->HostipLookupResultSet->gml:featureMember->Hostip->countryName but it seems it is wrong.
This might work if you query using xpath :-
// optional for register namespace into xpath
$xml->registerXPathNamespace('gml', 'http://www.opengis.net/gml');
$result = $xml->xpath('//*[self::countryName or self::countryAbbrev]');
You need to add the namespace.
See the code bellow
/* #var $xml SimpleXMLElement */
echo $xml->getName() . "\n";
$namespaces = $xml->getDocNamespaces();
foreach($xml->children($namespaces['gml']) as $child) {
echo $child->getName() . ": " . $child . "\n";
}
You can try the following code for getting Country and Country Abbrevation:
$xml = simplexml_load_file("http://api.hostip.info/?ip=12.215.42.19");
$cntry= $xml->xpath('//gml:featureMember');
foreach($cntry as $child) {
echo $child->Hostip->countryName;
echo "<br />";
echo $child->Hostip->countryAbbrev;
}

parsing a xml to get some values

http://www.managerleague.com/export_data.pl?data=transfers&output=xml&hide_header=0
These are player sales from a browser game. I want to save some fields from these sales. I am fetching that xml with curl and storing on my server. Then do the following:
$xml_str = file_get_contents('salespage.xml');
$xml = new SimpleXMLElement($xml_str);
$items = $xml->xpath('*/transfer');
print_r($items);
foreach($items as $item) {
echo $item['buyerTeamname'], ': ', $item['sellerTeamname'], "\n";
}
The array is empty and i cant seem to get anything from it. What am i doing wrong?
There is no reason to use cURL or XPath for that. You can do
$url = 'http://www.managerleague.com/export_data.pl?data=transfers&output=xml&hide_header=0';
$transfers = new SimpleXMLElement($url, NULL, TRUE);
foreach($transfers->transfer as $transfer) {
printf(
"%s transfered from %s to %s\n",
$transfer->playerName,
$transfer->sellerTeamname,
$transfer->buyerTeamname
);
}
Live Demo
You forgot a slash in your xpath:
$xml_str = file_get_contents('salespage.xml');
$xml = new SimpleXMLElement($xml_str);
$items = $xml->xpath('/*/transfer');
print_r($items);
foreach($items as $item) {
echo $item->buyerTeamname, ': ', $item->sellerTeamname, "\n";
}
<?php
$xml = simplexml_load_file("test.xml");
echo $xml->getName() . "<br />";
foreach($xml->children() as $child)
{
echo $child->getName() . ": " . $child . "<br />";
}
?>
Is this what you want?

Categories