i have used one xml file in that file there is one tag like
<image><![CDATA[ _abc.jpg ]]></image>
$xml = simplexml_load_file('test1.xml');
foreach($xml as $product)
{
echo $product->image;
}
Please tell me how to parse data with in php?
You could make of simplexml_load_string with these libxml options
<?php
$xml='<image><![CDATA[ _abc.jpg ]]></image>';
$xml = simplexml_load_string($xml,'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOBLANKS);
print_r($xml);
OUTPUT :
SimpleXMLElement Object
(
[0] => _abc.jpg
)
To just print the _abc.jpg , Just do this. echo $xml[0];
Related
Hi I want to parse in php
and read only one option of each line. How can I do this?
I tried:
<?php
$xml = file_get_contents("file.xml");
echo $xml;
?>
But this read the full xml. I need only one option of the xml
Try this
<? php $xml = simplexml_load_file("path/to/your/file/file.xml"); ?>
Convert your object into array like this
$array= (array) $xml;
after that you can do this
$option = $array['OptionName'];
Take a look at simplexml_load_file and turn your xml into a workable object where you can loop through and find whichever properties you need.
$xml = simplexml_load_file('file.xml');
foreach($xml->ParkingData as $k => $v)
echo $v->ParkingFacility->ParkingName ." - Vacant Spaces: ". $v->ParkingFacility->VacantSpaces."<br />";
I've got a problem when I want to convert an XML string into a SimpleXMLElement object.
This is my PHP code:
// XML string
$xmlStr = <<<XML
<?xml version='1.0'?>
<document>
<lastname lang="EN">Smith</lastname>
</document>
XML;
// Convert the XML string into an Array
$xml = simplexml_load_string($xmlStr);
$json = json_encode($xml);
$xmlArray = json_decode($json, true);
// Print SimpleXMLElement object
print_r($xml);
echo '<br /><br />';
// Print JSON
print_r($json);
echo '<br /><br />';
// Print XML Array
print_r($xmlArray);
echo '<br /><br />';
And I get this result:
SimpleXMLElement Object ( [lastname] => Smith )
{"lastname":"Smith"}
Array ( [lastname] => Smith )
But there is no "lang" attribute and I don't know what I'm doing wrong... :(
Anyone can help me, please? Any idea would be very appreciated! Thanks in advance.
To set the text value 'Smith' of the 'lastname' tag I do:
$xml->lastname->{'_'}
Hope it helps!
The json_encode function will not add xml attributes on any the node that is only text. If a final element is just text and you need to use another attribute as lang in this case, you'll have to add another enclosing mark. I always use _value, the same as do Mootools XmlToJsObject library.
I wrote a small PHP test .
I have an XML file that looks like the example on this site: http://msdn.microsoft.com/en-us/library/ee223815(v=sql.105).aspx
I am trying to parse the XML file using something like this:
$data = file_get_contents('http://mywebsite here');
$xml = new SimpleXMLElement($data);
$str = $xml->Author;
echo $str;
Unfortunately, this is not working, and I suspect it is due to the namespaces. I can dump the $xml using asXML() and it correctly shows the XML data.
I understand I need to insert namespaces somehow, but I'm not sure how. How do I parse this type of XML file?
All you need is to register the namespace
$sxe = new SimpleXMLElement($data);
$sxe->registerXPathNamespace("diffgr", "urn:schemas-microsoft-com:xml-diffgram-v1");
$data = $sxe->xpath("//diffgr:diffgram") ;
$data = $data[0];
echo "<pre>";
foreach($data->Results->RelevantResults as $result)
{
echo $result->Author , PHP_EOL ;
}
Output
Ms.Kim Abercrombie
Mr.GustavoAchong
Mr. Samuel N. Agcaoili
See Full code In Action
I've got an XML output that produces code such as:
<loadavg>
<one>0.00</one>
<five>0.02</five>
<fifteen>0.02</fifteen>
</loadavg>
<!-- whostmgrd -->
I would like to know how I can use PHP to parse that file, and grab the contents between <one>,<five> and <fifteen>. It would be useful if it were to be stored as $loadavg[1] an array.
Thanks
Yep, SimpleXML can do it:
$xml = <<<XML
<loadavg>
<one>0.00</one>
<five>0.02</five>
<fifteen>0.02</fifteen>
</loadavg>
XML;
$root = new SimpleXMLElement($xml);
$loadavg = array((string) $root->one,
(string) $root->five,
(string) $root->fifteen);
print_r($loadavg);
prints
Array (
[0] => 0.00
[1] => 0.02
[2] => 0.02
)
The easiest way to parse an XML file is using SimpleXML. It should be easy to load the XML as a SimpleXML object and get the data using that. If you could post a sample of a complete XML file I could offer some sample code.
I have an xml file
<?xml version="1.0" encoding="utf-8"?>
<xml>
<events date="01-10-2009" color="0x99CC00" selected="true">
<event>
<title>You can use HTML and CSS</title>
<description><![CDATA[This is the description ]]></description>
</event>
</events>
</xml>
I used xpath and and xquery for parsing the xml.
$xml_str = file_get_contents('xmlfile');
$xml = simplexml_load_string($xml_str);
if(!empty($xml))
{
$nodes = $xml->xpath('//xml/events');
}
i am getting the title properly, but iam not getting description.How i can get data inside
the cdata
SimpleXML has a bit of a problem with CDATA, so use:
$xml = simplexml_load_file('xmlfile', 'SimpleXMLElement', LIBXML_NOCDATA);
if(!empty($xml))
{
$nodes = $xml->xpath('//xml/events');
}
print_r( $nodes );
This will give you:
Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[date] => 01-10-2009
[color] => 0x99CC00
[selected] => true
)
[event] => SimpleXMLElement Object
(
[title] => You can use HTML and CSS
[description] => This is the description
)
)
)
You are probably being misled into thinking that the CDATA is missing by using print_r or one of the other "normal" PHP debugging functions. These cannot see the full content of a SimpleXML object, as it is not a "real" PHP object.
If you run echo $nodes[0]->Description, you'll find your CDATA comes out fine. What's happening is that PHP knows that echo expects a string, so asks SimpleXML for one; SimpleXML responds with all the string content, including CDATA.
To get at the full string content reliably, simply tell PHP that what you want is a string using the (string) cast operator, e.g. $description = (string)$nodes[0]->Description.
To debug SimpleXML objects and not be fooled by quirks like this, use a dedicated debugging function such as one of these: https://github.com/IMSoP/simplexml_debug
This could also be another viable option, which would remove that code and make life a little easier.
$xml = str_replace("<![CDATA[", "", $xml);
$xml = str_replace("]]>", "", $xml);