PHP - Extract node value from nested xml file - php

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 ;

Related

Elements values empties parsing XML with PHP

The xml file to parse https://webassos.com/uploads/results/ffnex_performances_54059.xml
<?php
$file = 'https://webassos.com/uploads/results/ffnex_performances_54059.xml';
$xml = simplexml_load_file($file);
print_r($xml);
?>
Return a well formated array with XML elements and attributes
<?php
$meeting = $xml->MEET;
print_r($meeting);
?>
Return an empty object.
Same result with a DOM document and trying to get element MEET by tagName.
Why this method that I usually use does not work in this case, please?
Please check your structure You have a child node meets and there you find meet
var_dump($xml->MEETS->MEET);

How to get a particular value from a xml tag

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);
}

Simple xml returns no values when trying to access nodes

Hi guys very new to the php world.
I am listening for a PHP post that contains xml, when the xml is retrieved i need to access individual nodes. I am able to echo the full xml file but not individual attributes.
Currently I am just sending the data using a chrome extension Postman. There is no front end code. Here is my XML:
<?xml version="1.0" encoding="UTF-8"?>
<job>
<job_ref>abc123</job_ref>
<job_title>Test Engineer</job_title>
</job>
And here is my PHP:
if($_SERVER['REQUEST_METHOD'] === 'POST') {
$xml = file_get_contents('php://input');
echo $xml;
$xml=simplexml_load_file($xml);
echo $xml->job_ref . "<br>";
echo $xml->job_title . "<br>";
}else{
die();
}
Any hep wopuld be amazing am I am very stuck.
Many thanks
simplexml_load_file expect PATH to the XML file, not its content. You have to use simplexml_load_string instead:
$xml = simplexml_load_string($xml);

Simplexml not displaying info

I have made a search html page using IMDBAPI to create an xml file for that item I search. Then I have a php file that will parse through that file and only pull the data I want from the xml file to display. Everything seems to be working as I am getting a blank white page. The issue is that nothing is displaying.
Part of the XML
<IMDBDocumentList>
<item>
<rating>8.7</rating>
<rating_count>10301</rating_count>
<year>1999</year>
<genres>
<item>Animation</item>
<item>Action</item>
<item>Adventure</item>
<item>Comedy</item>
<item>Drama</item>
<item>Fantasy</item>
<item>Romance</item>
</genres>
<rated>PG</rated>
<title>Wan pîsu: One Piece</title>
<imdb_url>http://www.imdb.com/title/tt0388629/</imdb_url>
<directors>
<item>Kônosuke Uda</item>
</directors>
<actors>
<item>Mayumi Tanaka</item>
<item>Kazuya Nakai</item>
<item>Akemi Okamura</item>
<item>Kappei Yamaguchi</item>
<item>Mahito Ôba</item>
<item>Hiroaki Hirata</item>
<item>Colleen Clinkenbeard</item>
<item>Ikue Ôtani</item>
<item>Chikao Ôtsuka</item>
<item>Yuriko Yamaguchi</item>
<item>Luci Christian</item>
<item>Christopher Sabat</item>
<item>Sonny Strait</item>
<item>Kazuki Yao</item>
</actors>
PHP Parse
$lib = simplexml_load_file("test.xml");
$xml = $lib->IMDBDocumentList->item;
$rating = $xml->rating;
$year = $xml->year;
print $rating;
print $year;
IMDBDocumentList is your root node, so you don't need to address it:
$xml = $lib->item;

Modify xml nodes using DOM or SIMPLE XML?

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++;
}
?>

Categories