I am currently working on a project that requires me to query an XML file like php to return a value that matches the request. Take a look at the XML:
<ENVELOPE>
<MASTER>
<STKDETNAME>004-011</STKDETNAME>
<STKPNO>PTN771</STKPNO>
<STKPRICE></STKPRICE>
<STKOPBAL>500</STKOPBAL>
</MASTER>
<MASTER>
<STKDETNAME>004-012</STKDETNAME>
<STKPNO>PTN772</STKPNO>
<STKPRICE></STKPRICE>
<STKOPBAL>500</STKOPBAL>
</MASTER>
<MASTER>
<STKDETNAME>004-013</STKDETNAME>
<STKPNO>PTN773</STKPNO>
<STKPRICE></STKPRICE>
<STKOPBAL>1000</STKOPBAL>
</MASTER>
<MASTER>
<STKDETNAME>004-014</STKDETNAME>
<STKPNO>PTN774</STKPNO>
<STKPRICE></STKPRICE>
<STKOPBAL>1000</STKOPBAL>
</MASTER>
<MASTER>
<STKDETNAME>004-015</STKDETNAME>
<STKPNO>PTN775</STKPNO>
<STKPRICE>400</STKPRICE>
<STKOPBAL>1000</STKOPBAL>
</MASTER>
</ENVELOPE>
Now, I want to get the STKPRICE AND STKOPBAL for a SKTPNO= PTN773. This is what i have seen so far, but i don't know how to get the two values. I am new to XML.
$file = 'stocksum.xml';//same file as above
$xmlfile = simplexml_load_file($file);
$partno = PTN775;
$fnd = $xmlfile->xpath('/ENVELOPE/MASTER/STKPNO[.="$partno"]');
There are a couple of issues with the code which are just syntax problems, these are the partno needing quotes and when building the XPath expression, you use single quotes so it doesn't insert the actual part number.
BUT to get to your actual problem, if you change your XPath to the one used here, this will find the <MASTER> element whose <STKPNO> is the one your after. So then you can refer to the elements withing the <MASTER> element using standard SimpleXML object notation...
$partno = 'PTN775';
$fnd = $xmlfile->xpath('/ENVELOPE/MASTER[STKPNO="'.$partno.'"]');
echo $fnd[0]->STKPRICE.PHP_EOL;
Note that as xpath() returns a list of matches, I use $fnd[0] to get the first one.
Code which also has a check to see if the part actually exists...
$xmlfile = simplexml_load_file($file);
$partno = 'PTN7751';
$fnd = $xmlfile->xpath('/ENVELOPE/MASTER[STKPNO="'.$partno.'"]');
if ( count($fnd) == 0 ) {
echo "Not found";
}
else {
echo $fnd[0]->STKPRICE.PHP_EOL;
}
Related
I have some problems with echo line from xml file.
How i can do it fine?
I try to
$test = file_get_contents('');
$test = iconv('WINDOWS-1251', 'UTF-8', $test);
$test = "<xmp>".$test."</xmp>";
And try to find with preg_match_all, but it isn't work.
preg_match_all('/<ya:created dc:date="\d+\-\d+\-\d+\T\d+\:\d+\:\d+/', $test, $output_array);
It's work on https://www.phpliveregex.com/ but isn't work on my site.
https://www.phpliveregex.com/p/qCH
My XML:
<?xml version="1.0" encoding="WINDOWS-1251"?>
<rdf:RDF
xml:lang="ru"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:foaf="http://xmlns.com/foaf/0.1/"
xmlns:ya="http://blogs.yandex.ru/schema/foaf/"
xmlns:img="http://blogs.yandex.ru/schema/foaf/"
xmlns:dc="http://purl.org/dc/elements/1.1/">
<foaf:Person>
<ya:publicAccess>allowed</ya:publicAccess>
<foaf:gender>male</foaf:gender>
<ya:created dc:date="2011-01-30T16:43:45+03:00"/>
<ya:lastLoggedIn dc:date="2019-01-16T18:54:55+03:00"/>
<ya:modified dc:date="2019-01-13T21:15:43+03:00"/>
</foaf:Person>
</rdf:RDF>
You would be better accessing it using something like SimpleXML and XPath. There are at least two ways of doing it, both here rely on using XPath but you have to use the namespaces (the ya: bit) to ensure you get the right element. As XPath returns a list of matches, I just use [0] to get the first one, if there are multiple ones you can use a loop...
$test = file_get_contents("data.xml");
$xml = simplexml_load_string($test);
// Version 1
// Fetch the ya:created element
$created = $xml->xpath("//ya:created")[0];
// Extract the attributes and print the date
echo $created[0]->attributes("http://purl.org/dc/elements/1.1/")->date;
// Version 2
// Extract the dd:date attribute (using the #)
$createdDate = $xml->xpath("//ya:created/#dc:date")[0];
echo $createdDate;
Forgot to say - if you want to use these fields for a database etc. you may need to cast them to a string to make sure they are converted...
$date = (string)$createdDate;
I am working with PHP and SimpleXML/XPath, and I'm just wondering how to set a certain parent (with a certain attribute value) equal to a variable, which I could use in a 'foreach'?
I'm currently getting this error:
Notice: Array to string conversion
and this output
Array
Thanks for any leads.
Here is the php code:
<?php
$url = "test_b.xml";
$xml = simplexml_load_file($url);
$xml_report_abbrev_b = $xml->xpath('//poster[#name="U-Verify"]')[0];
if($xml_report_abbrev_b){
foreach($xml_report_abbrev_b as $node_a) {
echo '<h1>'.$node_a->xpath('/full_image/#url').'</h1>';
}
} else {
echo 'XPath query failed';
}
?>
Here's the xml:
<data>
<poster name="U-Verify" id="uverify">
<full_image url="u-verify.jpg"/>
<full_other url=""/>
</poster>
<poster name="Minimum" id="min">
<full_image url="min.jpg"/>
<full_other url="spa_min.jpg"/>
</poster>
</data>
Using SimpleXML's element access and attribute access directly instead of using the XPath query will make the code simpler and perform better.
Your code could be reduced to...
$xml_report_abbrev_b = $xml->xpath('//poster[#name="U-Verify"]');
if($xml_report_abbrev_b){
echo '<h1>'.$xml_report_abbrev_b[0]->full_image['url'].'</h1>';
} else {
echo 'XPath query failed';
}
Note the way the echo line says - with the <poster> element you found from the XPath expression, use the <full_image> element and fetch the url attribute.
I also moved the [0] into the if because if the XPath didn't find a value, this produced an error as there isn't any data to get a value from.
This outputs...
<h1>u-verify.jpg</h1>
I have got this xml structure below. For every 'locatie' (in every 'land') I need the 'id' value and it's 'sneeuwkwaliteit'.
My effort so far, does not return the value of 'sneeuwkwaliteit':
$sneeuw = simplexml_load_file('ski.xml');
echo $sneeuw->land[0]->locaties[0]->sneeuw->{'ski'}->sneeuwkwaliteit;
<sneeuw>
<aanmaak_tijd>09-01-2016 07:48</aanmaak_tijd>
<landen>
<land id="Andorra">
<locaties>
<locatie id="Arinsal/Pal">
<ski>
<datum_tijd>09-01-2016</datum_tijd>
<sneeuwhoogte_dal>40</sneeuwhoogte_dal>
<sneeuwhoogte_berg>70</sneeuwhoogte_berg>
<sneeuwkwaliteit>De aanwezige sneeuw is poedersneeuw.</sneeuwkwaliteit>
<datum_laatste_sneeuwval>07-01-2016</datum_laatste_sneeuwval>
<hoeveelheid_laatste_sneeuwval>20</hoeveelheid_laatste_sneeuwval>
<totaal_aantal_liften>25</totaal_aantal_liften>
<aantal_liften_geopend>25</aantal_liften_geopend>
</ski>
</locatie>
</locaties>
</land>
</landen>
</sneeuw>
Just carefully follow the path from the root of your XML to the target element :
$xml = <<<XML
<sneeuw>
<aanmaak_tijd>09-01-2016 07:48</aanmaak_tijd>
<landen>
<land id="Andorra">
<locaties>
<locatie id="Arinsal/Pal">
<ski>
<datum_tijd>09-01-2016</datum_tijd>
<sneeuwhoogte_dal>40</sneeuwhoogte_dal>
<sneeuwhoogte_berg>70</sneeuwhoogte_berg>
<sneeuwkwaliteit>De aanwezige sneeuw is poedersneeuw.</sneeuwkwaliteit>
<datum_laatste_sneeuwval>07-01-2016</datum_laatste_sneeuwval>
<hoeveelheid_laatste_sneeuwval>20</hoeveelheid_laatste_sneeuwval>
<totaal_aantal_liften>25</totaal_aantal_liften>
<aantal_liften_geopend>25</aantal_liften_geopend>
</ski>
</locatie>
</locaties>
</land>
</landen>
</sneeuw>
XML;
$sneeuw = simplexml_load_string($xml);
echo $sneeuw->landen[0]->land[0]->locaties[0]->locatie[0]->ski[0]->sneeuwkwaliteit;
eval.in demo
output :
De aanwezige sneeuw is poedersneeuw.
For more complex query against XML, look into XPath, which has it's own specification. Then you can execute XPath expression using SimpleXMLElement::xpath() function.
UPDATE :
You can use XPath, as mentioned above, to iterate through all sneeuwkwaliteit elements located in the same path :
....
$result = $sneeuw->xpath('/sneeuw/landen/land/locaties/locatie/ski/sneeuwkwaliteit');
foreach($result as $r){
echo $r ."<br>";
}
I have a page in php where I have to parse an xml.
I have done this for example:
$hotelNodes = $xml_data->getElementsByTagName('Hotel');
foreach($hotelNodes as $hotel){
$supplementsNodes2 = $hotel->getElementsByTagName('BoardBase');
foreach($supplementsNodes2 as $suppl2) {
echo'<p>HERE</p>'; //not enter here
}
}
}
In this code I access to each hotel of my xml, and foreach hotel I would like to search the tag BoardBase but it doesn0t enter inside it.
This is my xml (cutted of many parts!!!!!)
<hotel desc="DESC" name="Hotel">
<selctedsupplements>
<boardbases>
<boardbase bbpublishprice="0" bbprice="0" bbname="Colazione Continentale" bbid="1"></boardbase>
</boardbases>
</selctedsupplements>
</occupancy></occupancies>
</hotel>
I have many nodes that doesn't have BoardBase but sometimes there is but not enter.
Is possible that this node isn't accessible?
This xml is received by a server with a SoapClient.
If I inspect the XML printed in firebug I can see the node with opacity like this:
I have also tried this:
$supplementsNodes2 = $hotel->getElementsByTagName('boardbase');
but without success
2 issues I can see from the get-go: XML names are case-sensitive, hence:
$hotelNodes = $xml_data->getElementsByTagName('Hotel');
Can't work, because your xml node looks like:
<hotel desc="DESC" name="Hotel">
hotel => lower-case!
As you can see here:
[...] names for such things as elements, while XML is explicitly case sensitive.
The official specs specify tag names as case-sensitive, so getElementsByTagName('FOO') won't return the same elements as getElementsByTagName('foo')...
Secondly, you seem to have some tag-soup going on:
</occupancy></occupancies>
<!-- tag names don't match, both are closing tags -->
This is just plain invalid markup, it should read either:
<occupancy></occupancy>
or
<occupancies></occupancies>
That would be the first 2 ports of call.
I've set up a quick codepad using this code, which you can see here:
$xml = '<hotel desc="DESC" name="Hotel">
<selctedsupplements>
<boardbases>
<boardbase bbpublishprice="0" bbprice="0" bbname="Colazione Continentale" bbid="1"></boardbase>
</boardbases>
</selctedsupplements>
<occupancy></occupancy>
</hotel>';
$dom = new DOMDocument;
$dom->loadXML($xml);
$badList = $dom->getElementsByTagName('Hotel');
$correctList = $dom->getElementsByTagName('hotel');
echo sprintf("%d",$badList->lenght),
' compared to ',
$correctList->length, PHP_EOL;
The output was "0 compared to 1", meaning that using a lower-case selector returned 1 element, the one with the upper-case H returned an empty list.
To get to the boardbase tags for each hotel tag, you just have to write this:
$hotels = $dom->getElementsByTagName('html');
foreach($hotels as $hotel)
{
$supplementsNodes2 = $hotel->getElementsByTagName('boardbase');
foreach($supplementsNodes2 as $node)
{
var_dump($node);//you _will_ get here now
}
}
As you can see on this updated codepad.
Alessandro, your XML is a mess (=un casino), you really need to get that straight. Elias' answer pointed out some very basic stuff to consider.
I built on the code pad Elias has been setting up, it is working perfectly with me:
$dom = new DOMDocument;
$dom->loadXML($xml);
$hotels = $dom->getElementsByTagName('hotel');
foreach ($hotels as $hotel) {
$bbs = $hotel->getElementsByTagName('boardbase');
foreach ($bbs as $bb) echo $bb->getAttribute('bbname');
}
see http://codepad.org/I6oxkEOC
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++;
}
?>