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 />";
Related
I have I'm trying to get the "extract" node value from an xml file/url. Below is my code, but I'm not getting any output.
<?php
$url = "https://en.wikipedia.org/w/api.php?format=xml&action=query&prop=extracts&exintro=&explaintext=&titles=unix";
$xml = simplexml_load_file($url);
echo $xml->extract ;
?>
Any help would be greatly appreciated.
$xml->extract would work if the node was a direct child of the xml file.
After looking at the api response, I am able to get and display the extract node using the full path:
$url = "https://en.wikipedia.org/w/api.php?format=xml&action=query&prop=extracts&exintro=&explaintext=&titles=unix";
$xml = simplexml_load_file($url);
echo $xml->query->pages->page->extract ;
I have to parse a XML file and create a drop-down list in my web application.
Right now, after parsing the XML the output looks something like -
scripts/testSuite/networkSecurity/802dot1x/802dot1xBasicFunctionality/802dot1xRadAccAVPClntStatIp.tcl
scripts/testSuite/networkSecurity/802dot1x/802dot1xConfiguration/802dot1xBasicUserMode.tcl
script is one of the tag of my XML file.
Now from this tag I just want the 2nd position (networkSecurity) nad last and print them.
How Can I achieve this ?
Try this:
$parser = xml_parser_create();
xml_parse_into_struct($parser, $xml, $values, $indexes);
xml_parser_free($parser);
where $xml is your xml, $values and $indexes will be the returned arrays containing the xml tags found and their position.
You can use simplexml_load_file — Interprets an XML file into an object
$xml = simplexml_load_file("file.xml");
echo $xml->data;
*UPDATE
to get a specific value by slash delimiter:
$xml = simplexml_load_file("file.xml");
foreach($xml->testcase as $var){
$var=explode('/',$var->script);
echo $var[2] ."\n". end($var);
}
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 have source XML here: http://www.grilykrby.cz/rss/pf-heureka.xml. I want to use this xml feed and create another modified on my own server. I would like to change every node CATEGORYTEXT which contains word Prislusenstvi. I just tried something but I got only the listing of all categories without changing XML :-(
Here is the example of my code. The row $kategorie="nejaka kategorie"; doesn't work.
<?php
$file = "http://www.grilykrby.cz/rss/pf-heureka.xml";
$xml=simplexml_load_file($file);
foreach ($xml->xpath('//SHOPITEM/CATEGORYTEXT') as $kategorie) {
echo $kategorie."<br />";
$kategorie="nejaka kategorie";
}
file_put_contents('test.xml', $xml->asXML());
?>
$kategorie is just a temp variable used in the loop which contains a copy of the data returned by xpath query. You would need to actually set the value directly in the $xml object.
I would personally also consider doing a str_replace or preg_replace within the XML content itself before parsing it into a simpleXML object.
Final Accepted Answer
<?php
$xml = simplexml_load_file('http://www.grilykrby.cz/rss/pf-heureka.xml');
$i=0;
foreach($xml -> SHOPITEM as $polozka) {
if ($polozka -> CATEGORYTEXT == "Příslušenství") $xml -> SHOPITEM[$i] -> CATEGORYTEXT = "Some other text";
$i++;
}
?>
I dont seem to quite understand how xml_parse works. What i was doing was getting the contents of the xml file, create a parser, and pass it into xml_parse(). I dont know what to do after that. I was thinking that in my case, $xml was the array i can now iterate through. I was trying to see how i would parse my data.
$fp = file_get_contents("memberdata.xml");
echo "pre-create";
$xml = xml_parser_create();
echo "pre-parse";
$status = xml_parse($xml,$fp);
if(!$status){
die("Error parsing data from $fp into $xml");
}
echo "pre XML posting";
echo "<br />".$xml."<br />";
print_r($xml);
xml_parser_free($xml);
I cant seem to figure out how to access this.
Sample xml data is as follows:
<currentTime>2012-09-05 03:43:25</currentTime>
<result>
<rowset name="members" key="characterID" columns="characterID,name,startDateTime,baseID,base,title,logonDateTime,logoffDateTime,locationID,location,shipTypeID,shipType,roles,grantableRoles">
<row ..>
</rowset>
</result>
<cachedUntil></cashedUntil>
Instead of using the base tools, using one of the toolkits, SimpleXML will alleviate a lot of the frustrations. The answer to this question is redone using SimpleXML.
$fp = file_get_contents("memberdata.xml");
$eve = new SimpleXMLElement($fp);
$cols = $eve->{'result'}->rowset['columns'];
//I put result in {} because result is a special character in php.
$cols = explode(",",$cols);
foreach ($cols as $c){
echo "".$c."<br />";
}
//output is printed to screen