I am trying parse the following XML weather forecast data using the PHP simpleXML extension:
http://www.pakenhamweather.info/test/IDV10753.xml
I have tried a number of times to parse this XML but to no avail. The following is an example:
$product = simplexml_load_file("http://www.pakenhamweather.info/test/IDV10753.xml");
foreach ($product->forecast->area[2]->forecast-period[0] as $blah) {
printf(
"<p>Forecast Icon: $s</p><p>Precipitation Range: $s</p>"
$blah->element["forecast_icon_code"]
$blah->element["precipitation_range"]
);
}
For the purposes of this example, I have simplified the example by selecting 'area[2]' and 'forecast-period[0]', but ideally, I would like to specify the 'area' by the 'description' attribute and 'forecast-period' based on the 'index' attribute.
Any help would be greatly appreciated. Thank you in advance.
You can try the following code to get area by attribute and forecast-period by attribute.
$product = simplexml_load_file("http://www.pakenhamweather.info/test/IDV10753.xml");
$forecasts = $product->forecast->xpath("//area[#description='Mildura']/forecast-period[#index='1']");
foreach ($forecasts as $forecast) {
$forecast_icon_code = $forecast->xpath("element[#type='forecast_icon_code']")[0];
$precipitation_range = $forecast->xpath("element[#type='precipitation_range']")[0];
echo $forecast_icon_code . "===" . $precipitation_range;
}
Just replace the attribute values which you want to pick
Related
Exemple with a mediawiki link : https://www.visionduweb.eu/wiki/index.php?title=Utiliser_PHP
Show the source code and identify the sommaire from this Mediawiki page.
I search how i can parse the source code and found the HTML code for this sommaire.
#
I tried with $domExemple = $xpath->query(« //ul/li »); but I have too many answers and poorly formatted.
I tried with $domExemple = $xpath->query(« //ul/li[#class=’toclevel-1 tocsection-1′] »); which gives me the result, but, how to get all toclevel and tocsection, without having to specify the number 1, or 2, or 3, ... toclevel or tocsection.
In this example, I do not get the HTML content, only the text content.
I would have preferred to retrieve the HTML content.
I believe you can simplify your xpath expression using the syntax defined here:
How can I match on an attribute that contains a certain string?
Try something like this:
$results = $xpath->query('//ul/li[contains(#class, "toclevel-") and contains(#class, "tocsection-"]');
foreach ($results as $li) {
// to get html of $li, import it into a fresh DOMDocument and run saveHTML
$newdoc = new DOMDocument();
$cloned = $li->cloneNode(true);
$newdoc->appendChild($newdoc->importNode($cloned, true));
echo $newdoc->saveHTML();
}
Currently I am working on a project which requires me to parse some data from an alternative website, and I'm having some issues (note I am very new to PHP coding.)
Here's the code I am using below + the content it returns.
$dl = $html2->find('ol.tracklist',0);
print $dl = $dl->outertext;
The above code returns the data for what we're trying to get, it's below but extremely messy provided you would like to see click here.
However, when I put this in a foreach, it only returns one of the a href attributes at a time.
foreach($html2->find('ol.tracklist') as $li)
{
$title = $li->find('a',0);
print $title;
}
What can I do so that it returns all of the a href elements from the example code above?
NOTE: I am using simple_html_dom.php for this.
Based on the markup, just point directly to it, just get it list then point to its anchor:
foreach ($html2->find('ol.tracklist li') as $li) {
$anchor = $li->find('ul li a', 0);
echo $anchor->href; // and other attributes
}
I am trying to access the xml attributes using php. I can access the actual text of any field, but
For example, a line from the xml file looks like this:
<Competitor CODE1="MIN" CODE3="MIN" CODE4="l.mlb.com-t.10" ID="69" LineOrderOverride="" NAME="Minnesota Twins" NUM="1" ROT="927" SCORE="5">Game 1</Competitor>
The code I am using will show me "Game 1" but I want to be able to access the NAME and SCORE values
#$doc->load($feedURL);
$arrFeeds = array();
foreach ($doc->getElementsByTagName('Event') as $node) {
$itemRSS = array (
echo strip_tags(str_replace("'","",$node->getElementsByTagName('Competitor')->item(0)->nodeValue));
};
Does anyone know a way to access the meta variables using PHP?
Inside your foreach(), add
echo $node->getElementsByTagName('Competitor')->item(0)->getAttribute("NAME"); // For NAME
echo $node->getElementsByTagName('Competitor')->item(0)->getAttribute("SCORE"); // For SCORE
Using Zend_Dom_Query, I would like to retrieve meta data out of an HTML string.
to retrieve links you can simply query like so:
$results = $dom->query('a'); //Where $dom is your html string
Unfortunately this doesn't seem to work with meta
$results = $dom->query('meta'); //doesn't work
How can I retrieve meta data and then filter by its 'property' attribute?
An example of what I'm looking for:
public function meta($dom)
{
$results = $dom->query('meta'); //This is not a correct query (does anyone have an alternative?)
$links = array();
foreach ($results as $index => $result) {
if ($result->getAttribute('property') == 'title') { //find <meta property="title"
echo $result->getAttribute('content') . '<br />'; //echo the content attribute of the title
}
}
return $results;
}
This code would work once the query is correct. However I would like to take it a step further and query directly for <meta property="title" content="This is the Title" /> instead of retrieving all meta and looping around to get the right one.
Any help with either getting all meta data using zend_dom_query or (more importantly) querying to receive only the meta data where property == title would be appreciated.
Thanks
the meta tag in not a valid CSS selector so you have to use the $dom->queryXpath($xPathQuery) method instead of $dom->query().
maybe something like:
$dom->queryXpath('/html/head');
I'm not sure of the exact query string to use, but this is the idea.
Zend_Dom_Query Theory of Operation.
if you have got url try this:
$metatagarray = get_meta_tags($url);
if (!empty($metatagarray["keywords"]))
$metakey = $metatagarray["keywords"];
if (!empty($metatagarray["description"]))
$metadesc = $metatagarray["description"];
I have an XML document that I am trying to get some of the values for and don't know how to get to the attributes. An example of the structure and values are below:
<vin_number value="3N1AB51D84L729887">
<common_data>
<engines>
</engines>
</common_data>
<available_vehicle_styles>
<vehicle_style name="SE-R 4dr Sedan" style_id="100285116" complete="Y">
<engines>
<engine brand="" name="ED 2L NA I 4 double overhead cam (DOHC) 16V"></engine>
</engines>
</vehicle_style>
</available_vehicle_styles>
</vin_number>
I am trying to get the engine["name"] attribute (NOT "ENGINES"). I thought the following would work but I get errors (I cant parse past "vehicle_style")
$xml = simplexml_load_file($fileVIN);
foreach($xml->vin_number->available_vehicle_styles->vehicle_style->engines->engine->attributes() as $a => $b) {
echo $b;
}
Assuming your XML is structured in the same was as this example XML, the following two snippets will get the engine name.
The property hierarchy way (split onto multiple lines so you can read it).
$name = (string) $xml->vin_number
->available_vehicle_styles
->vehicle_style
->engines
->engine['name'];
Or the more concise XPath way.
$engines = $xml->xpath('//engines/engine');
$name = (string) $engines[0]['name'];
Unless there are multiple engine names in your XML, there is no need to use a foreach loop at all.
(See both snippets running on a codepad.)
Use the SimpleXMLElement::attributes method to get the attributes:
foreach($xml->available_vehicle_styles->vehicle_style as $b) {
$attrs = $b->attributes();
echo "Name = $attrs->name";
}
Note: I slightly changed the "path" to the element starting from $xml because that's how it loaded the fragment for me.
By this layout, there could be more than one engine per engines block, so you have to explicitly pick the first one. (Assuming you know for sure there's only going to be one.)
$name = $xml->available_vehicle_styles->vehicle_style->engines->engine[0]->attributes()->name;