PHP - Xpath Variable Help - To Get Dom-document Up And Running
Similar TutorialsSo here's what I'm trying to do, and I haven't found any clear tutorials on how to properly navigate a DOMDocument object, at least not in the strict sense of PHP. I'm building a web scraper, I've had it working for some time now using more traditional methods (a combination of string manipulation and clever regex). I've been told xpath can be much faster and more reliable for what I need. Sold. Let's say I'm parsing a forum. This forum separates each reply in a post with a set of <li></li> with a class of "message" Code: [Select] <li class="message"> // Stuff here </li> <li class="message"> // Stuff here </li> So far so good. These list items contain all the formatting for each post, including user info and the message text. Each sitting in it's own div. Code: [Select] <li class="message"> <div class="user info"> User info here </div> <div class="message text"> Message text here </div> </li> <li class="message"> <div class="user info"> User info here </div> <div class="message text"> Message text here </div> </li> Still with me? Good. With this bit of code I can select each message list item block and iterate over all the sub nodes inside. Code: [Select] $items = $xpath->query("//li[starts-with(@class, 'message')]"); for ($i = 0; $i < $items->length; $i++) { echo $items->item($i)->nodeValue . "\n"; } This produces a basic text dump of the entire forum. Close, but not what I need. What I'm trying to do is as follows Select all the class="message" list items [done] Once those have been selected, run another $xpath->query to select the child nodes which contain the user info and message text Step one is done, step two is what is confusing me. How can I run a new query based on the output from the first query? Thanks guys hello dear community, good day,
I've been having an issue trying to parse text in a span cass with DOM. Here is my code example. try to extract some lines out of a webpage - with following technique: with the Extraction of values of attributes of elements with DOMDocument. Here is what i have gathered and learned:
$remote = "http://website.com/"; $doc = new DOMDocument(); @$doc->loadHTMLFile($remote); $xpath = new DOMXpath($doc); $node = $xpath->query('//span[@class="user"]'); echo $node;
and this returns the following
error -> "Catchable fatal error: Object of class DOMNodeList could not be converted to string".
And now - with this i need help.
What I am trying to do is parse the user name between this tag;
<div class="widget plugin-meta"> <h3 class="screen-reader-text">Meta</h3>
see more below:Here the concrete example view-source: https://wordpress.org/plugins/participants-database/ and https://wordpress.org/plugins/participants-database/
goal i need the following data:
Version: Last updated: Active installations: Tested up:
view-source: https://wordpress.org/plugins/participants-database/
Proceedings; i checked the source of the webpage. i tried to find out whether the texte is related to some kind of pattern.i have looked closely and found that all of them have class=”widget plugin-meta” . Well - This will make extracting them, a piece of cake. I tried with the code below helps to filter html elements based on values of attributes. but unfortunatley this ends up in a bad result; i need a helping hand and need to know how to parse the above mentioned data
Version: Last updated: Active installations: Tested up:
Any idea for the starting-point!? I love to hear from you.
This is what I have $eeee = $_POST['forum_reply']; $eeee = strip_tags($eeee, '<p>'); echo $eeee; this is what prints out Quote Untitled document testinggg this is a print_r of $_Post Quote Array ( [forum_reply] => testinggg [form_reply_submit] => Reply ) Please someone tell me what in binary's name is going on! My parser: <?php if (isset($_GET['formSubmit'])) { $option = $_GET['option']; $option = array_values($option); if (!empty($option)){ $xml = simplexml_load_file('differences.xml'); $i = 0; $count = count($option); ?> <table> <tr> <td>Item</td> <td>Code</td> <td>Type</td> <td>Level</td> </tr> <?php while($i < $count) { $selected = $xml->xpath("//item[contains(@name,'".$option[$i]."')]"); echo "<tr>"; echo "<td>".$selected[0]['name']."</td>"; echo "<td>".$selected[0]['code']."</td>"; echo "<td>".$selected[0]['type']."</td>"; echo "<td>".$selected[0]['requiredLevel']."</td>"; echo "</tr>"; $i++; } ?> </table> This works great however requiredLevel is showing nothing. Here is what is stored in the $selected array: Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [buildingName] => EiffelTower [code] => lM [type] => building ) [requiredLevel] => 5 [cost] => 5000 [built] => 03/31/1889 => SimpleXMLElement Object ( [@attributes] => Array ( [itemClass] => EiffelTower_construct ) ) [defaultItem] => SimpleXMLElement Object ( [@attributes] => Array ( [amount] => 1 [name] => iron ) ) [finishedReward] => iron [image] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [loadClass] => mc [name] => construct_0 [url] => /buildings/eiffel_tower.swf ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [loadClass] => mc [name] => construct_1 [url] => /buildings/eiffel_tower.swf ) ) [2] => SimpleXMLElement Object ( [@attributes] => Array ( [loadClass] => mc [name] => built_0 [url] => /buildings/eiffel_tower.swf ) ) [3] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => icon [url] => buildings/eiffel_tower.png ) ) [4] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => startIcon [url] => /buildings/eiffel_tower.png ) ) [5] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => halfIcon [url] => /buildings/eiffel_tower.png ) ) [6] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => completeIcon [url] => /buildings/eiffel_tower.png ) ) ) [constructionUI] => /buildings/Construct_eiffel_tower.swf [countryCard] => france ) My XML: <element> <item code="lM" name="snowman2010" type="building"> <requiredLevel>5</requiredLevel> <cost>5000</cost> <built>03/31/1889</built> <storageType itemClass="EiffelTower_construct"/> <defaultItem amount="1" name="iron"/> <finishedReward>iron</finishedReward> <image loadClass="mc" name="construct_0" url="/buildings/eiffel_tower.swf"/> <image loadClass="mc" name="construct_1" url="/buildings/eiffel_tower.swf"/> <image loadClass="mc" name="built_0" url="/buildings/eiffel_tower.swf"/> <image name="icon" url="/buildings/eiffel_tower.png"/> <image name="startIcon" url="/buildings/eiffel_tower.png"/> <image name="halfIcon" url="/buildings/eiffel_tower.png"/> <image name="completeIcon" url="/buildings/eiffel_tower.png"/> <constructionUI>/buildings/eiffel_tower.swf</constructionUI> <countryCard>france</countryCard> </item> Using the established code pattern in my parser.php how can I print out requiredLevel? The output of my array is mighty confusing! [/code] Hi everyone, I am having trouble with the last() function in xpath . I want to get the text3. The HTML looks like this: Code: [Select] div id="body"> <br> <br> text1 <hr> text2 <br> <br> text3 So far I have: Code: [Select] $lastdata= $simplexml->xpath("//div[@id='body']/following-sibling::node [last()]"); This is not working. I would appreciate any help. Thank you in advance, Bill Hi all, I am pretty new to php and I am having an issue trying to load an XML document. When ever I try to use Xpath it negates all the code below the line, including the HTML, and returns a white page. here is my code: Code: [Select] <html> <head> <?php $xpath = new DOMXPath("structure.xml"); ?> <body> hello world </body> </html> I checked phpinfo() and I have both the DOM and XPath enables and installed. I have also tried using just DOM and that worked so it is only Xpath that is not working. Ideas? Thank you James S I am having trouble obtaining the second table in this HTML code: Code: [Select] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head><html> <head> <meta http-equiv="Pragma" content="no-cache"> <title>Entity Information</title> <link rel="stylesheet" href="documents/CORP/default.css" media="screen" type="text/css"> <link rel="stylesheet" href="documents/CORP/infotable.css" media="screen" type="text/css"> </head> <body> <div class="headings"> <h1 class="dos_heading1">Company Information </h1> <BR> <h2 class="dos_heading2"> </h2> <h3 class="page_heading">Company Information</h3> <p class="DtMsg">The information contained in this database is current through September 10, 2010.</p> </div> <hr noshade width="100%"> <center> <div class="highlight">Selected Company Name: 1 PARK ROW, LLC</div> <table summary="This table contains status information for the selected entity."> <caption> Selected Company Status Information </caption> <tr> <th>Current Company Name:</th> <td>1 PARK ROW, LLC</td> </tr> <tr> <th>Initial Filing Date:</th> <td>JANUARY 22, 1997</td> </tr> <tr> <th>County:</th> <td>NASSAU</td> </tr> <tr> <th>Jurisdiction:</th> <td>NEW JERSEY </td> </tr> <tr> <th>Entity Type:</th> <td>DOMESTIC LIMITED LIABILITY COMPANY</td> </tr> <tr> <th>Current Company Status:</th> <td>ACTIVE </td> </tr> </table> <BR> <table id="tblAddr" summary="This table contains address information for the selected entity."> <caption>Selected Entity Address Information</caption> <tr> <th scope="col" id="c1" class="leftalign"><span class="rmvbold">Address</span></th> </tr> <tr> <td headers="c1"> C/O ELECTRONICS INC.<br> 2355 PARK ROW<br> NEW BRUNSWICK, NEW YORK, 15538 </td> </tr> <tr> <th scope="col" id="c4" class="leftalign">Registered Agent</th> </tr> <tr> <td headers="c1"> NONE </td> </tr> </table> <br> <p style="width:50%"> </p> <div id="divStockContainer"> <h4 id="capStock">*Stock Information</h4> <div id="divStock"> <table id="tblStock" cellpadding="0" cellspacing="6"> <tr> <th># of Shares</th> <th>Type of Stock</th> <th>$ Value per Share</th> </tr> <tr> <td> </td> <td>No Information Available</td> <td> </td> </tr> </table> </div> <p id="pStockBlurb">*Stock information is applicable to domestic business corporations.</p> </div> <div id="divHistNmContainer"> <h4 id="capNmHist">Name History</h4> <div id="divNmHist"> <table id="tblNameHist" cellpadding="0" cellspacing="6"> <tr> <th class="FileDt">Filing Date</th> <th class="NameType">Name Type</th> <th class="CorpName">Company Name</th> </tr> <tr> <td class="FileDt">JAN 22, 1997</td> <td class="NameType">Actual</td> <td class="CorpName">1 PARK ROW, LLC</td> </tr> </table> </div> <p id="pFictName"> </p> </div> </center> </body> </html> <script language='javascript' src='https://a12.alphagodaddy.com/hosting_ads/gd01.js'></script></script> I can get the top part with this: /*** a new dom object ***/ $dom = new domDocument; /*** load the html into the object ***/ $dom->loadHTML($html2); /*** discard white space ***/ $dom->preserveWhiteSpace = false; /*** the table by its tag name ***/ $tables = $dom->getElementsByTagName('table'); /*** get all rows from the table ***/ $rows = $tables->item(0)->getElementsByTagName('tr'); /*** loop over the table rows ***/ foreach ($rows as $row) { /*** get each column by tag name ***/ $cols = $row->getElementsByTagName('td'); /*** echo the values ***/ echo $cols->item(0)->nodeValue.'<br />'; echo $cols->item(1)->nodeValue.'<br />'; echo $cols->item(2)->nodeValue; echo '<hr />'; } } Any help would be appreciated I dont even know where to start to echo the 2nd table in the html. Thanks. I am trying to parse a blogspot feed using xpath but it doesnt seem to be working with anything that I try. I am not sure if it is because of the namespaces or what but I was hoping someone could help me. Here is the code: $xml = simplexml_load_file('http://feeds.feedburner.com/blogspot/MKuf'); $next = $xml->xpath("//link[@rel='next']"); print_r($next); This is just returning an empty array and it should not be. I tried it doing just link or just entry and it still is returning empty. The only one I can run on it that works is *. Any help is appreciated. Code: [Select] $domdoc=new DOMDocument(); $domdoc->formatOutput=TRUE; $empty_cart_xml= '<Order> <Cart> <Items> <Item>1</Item> <Item>2</Item> <Item>3</Item> </Items> </Cart> </Order>'; $domdoc->loadXML($empty_cart_xml); print $domdoc->saveXML()."<hr/>"; //works up to this point $xpath=new DOMXPath($domdoc); $items=$xpath->query('Order/Cart/Items'); foreach($itemses AS $items) { $items->appendChild($domdoc->createElement('Item','4')); } print $domdoc->saveXML(); All I want to do is to add a new Item to Items. What am I doing wrong? Hello, I recently moved hosts and upgraded to PHP 7.4.10. Suddenly, one of my sites is having issues with XSL views. The software creator claims this is an issue with XSL in PHP. I am unsure of what to check to troubleshoot this. Would anyone have an opinion on my issue? If not, please let me know if I should be posting somewhere else altogether. Thanks in advance - Quote
xmlXPathCompOpEval: function function not found The view.xsl file: Quote
<xsl:value-of select="php:function( 'SobiPro::AlternateLink', $rssUrl, 'application/atom+xml', $sectionName )"/>
Hi all I am trying to get the lat and long properties from the googlemaps geocoding api. $xml=simplexml_load_file($url); if ($xml === false) { echo "Failed loading XML\n"; foreach(libxml_get_errors() as $error) { echo "\t", $error->message; } } else{ $path = $xml->xpath( "/GeocodeResponse/result/geometry/location"); $lat = $path->lat; $lng = $path->lng; echo $lat; echo $lng; $sql1 = "UPDATE tbl_clubs set lat='$lat',lng='$lng' where club_id =$id"; echo $sql1; $result1 = mysqli_query($dbConn,$sql1) or die($mysqli_error($dbConn)); } This is the XML I am using <GeocodeResponse> <status>OK</status> <result> <type>establishment</type> <type>point_of_interest</type> <type>stadium</type> <formatted_address>Aldridge Rd, Perry Barr, Birmingham B42 2ET, UK</formatted_address> <address_component> </address_component> <address_component> </address_component> <address_component> </address_component> <address_component> <long_name>West Midlands</long_name> <short_name>West Midlands</short_name> <type>administrative_area_level_2</type> <type>political</type> </address_component> <address_component> <long_name>England</long_name> <short_name>England</short_name> <type>administrative_area_level_1</type> <type>political</type> </address_component> <address_component> <long_name>United Kingdom</long_name> <short_name>GB</short_name> <type>country</type> <type>political</type> </address_component> <address_component> <long_name>B42 2ET</long_name> <short_name>B42 2ET</short_name> <type>postal_code</type> </address_component> <geometry> <location> <lat>52.5196698</lat> <lng>-1.8986236</lng> </location> <location_type>GEOMETRIC_CENTER</location_type> <viewport> <southwest> <lat>52.5183208</lat> <lng>-1.8999726</lng> </southwest> <northeast> <lat>52.5210188</lat> <lng>-1.8972746</lng> </northeast> </viewport> </geometry>
But I am getting the following response Notice: Trying to get property of non-object in /home/sites/1a/9/95f15f28a6/public_html/results/getLatLng.php on line 22 I am guessing my path is wrong, any guidance would be great Hi all, I'm sure I'm making this far more difficult than I need to... I've got an xml document... <library id="1"> <lname>Lib1</name> <book id="1"> <title>book 1</title> <author>Author 1</author> </book> <book id="2"> <title>book 2</title> <author>Author 2</author> </book> </library> I can access the nested elements using xpath like : $result = $xml->xpath('//book'); foreach($result as $books) { echo $books->title; } How would I do this for the attributes? If I specify I want the attributes in the xpath query I can't get at the elements... I'm trying to turn this xml file into a simple relational database... A library table and a book table. So I need the library id (attribute) and the name(nested element) for one insert query - and the book id, title, author and the library id for the other. Anyone got any idea? Cheers John hi, im trying to parse this xml file : xml.gamebookers.com/sport /football.xml_attr.xml i want to echo on my page like this : league name 1 match 1 of league 1 odds odds odds match 2 of league 1 odds odds odds . . league name 2 match 1 of league 2 odds odds odds match2 of league 2 odds odds odds . . this is my code so far <?php $xml=simplexml_load_file('http://xml.gamebookers.com/sports/football.xml_attr.xml'); $league=$xml->xpath('//event/..'); // leagues refers to element<group> $matches=$xml->xpath('//bettype[@name="Versus (with Draw)"]/..'); // shows matches name (team versus team) $odds=$xml->xpath('//event/bettype[@name="Versus (with Draw)"]/bet'); //odds of matches 1 x 2 $leaguecount=count($league); $matchescount=count($matches); $oddscount=count($odds); $i=0; while ($i<$leaguecount){ echo $league[$i]['name'].'<br>'; $i++; } echo '<br><br>'; $i=0; while ($i<$matchescount){ echo $matches[$i]['name'].'<br>'; $i++; } $i=0; while ($i<$oddscount){ echo $odds[$i]['odd'].'<br>'; $i++; } ?> it prints this: http://www.mh724.com/school/xp.php so i need something like that: echo league[$i][mathces[$i][odds[$i] // like arrays of array but it does not work of course any help will highly appreciated :shrug: with regardS good evening dear php-freaks,
hope you are all right.
My tactic is as follows:
1. i run requests to the openstreetmap-endpoint- (see below) and i try three different APIs per request isn't that easy on ressources and 2. I don't know how to work with the results that i gather from the OpenStreetmap-Endpoint
as of the first point: - workin on the endpoint of OSM-Overpass-API.... see my approach: <?php /** * OSM Overpass API with PHP SimpleXML / XPath * * PHP Version: 5.4 - Can be back-ported to 5.3 by using 5.3 Array-Syntax (not PHP 5.4's square brackets) */ // // 1.) Query an OSM Overpass API Endpoint // $query = 'node ["amenity"~".*"] (38.415938460513274,16.06338500976562,39.52205163048525,17.51220703125); out;'; $context = stream_context_create(['http' => [ 'method' => 'POST', 'header' => ['Content-Type: application/x-www-form-urlencoded'], 'content' => 'data=' . urlencode($query), ]]); # please do not stress this service, this example is for demonstration purposes only. $endpoint = 'http://overpass-api.de/api/interpreter'; libxml_set_streams_context($context); $start = microtime(true); $result = simplexml_load_file($endpoint); printf("Query returned %2\$d node(s) and took %1\$.5f seconds.\n\n", microtime(true) - $start, count($result->node)); // // 2.) Work with the XML Result //
so far so good: with this i get a bunch of data.....
<?xml version="1.0" encoding="UTF-8"?> <osm version="0.6" generator="Osmosis SNAPSHOT-r26564" xapi:planetDate="2014-10-07T15:42:02Z" xmlns:xapi="http://jxapi.openstreetmap.org/"> <node id="251652819" version="6" timestamp="2013-10-19T14:03:10Z" uid="1198089" user="GeorgeKaplan" changeset="18434238" lat="48.8527413" lon="2.3333559"> <tag k="dispensing" v="yes"/> <tag k="website" v="http://www.pharmacie-paris-citypharma.fr/"/> <tag k="name" v="Pharmacie Citypharma in downtown paris"/> <tag k="amenity" v="pharmacy"/> </node> <node id="251774849" version="9" timestamp="2014-07-28T21:28:26Z" uid="92075" user="Art Penteur" changeset="24411346" lat="48.8468043" lon="2.3696423"> <tag k="phone" v="0143430996"/> <tag k="dispensing" v="yes"/> <tag k="name" v="Pharmacie de la Rapée in downtown paris"/> <tag k="amenity" v="pharmacy"/> <tag k="opening_hours" v="Mo-Fr 08:30-20:30; Sa 09:00-19:30"/> </node> <node id="263262912" version="3" timestamp="2014-07-28T21:37:24Z" uid="92075" user="Art Penteur" changeset="24411346" lat="48.8446917" lon="2.3101829"> <tag k="dispensing" v="yes"/> <tag k="amenity" v="pharmacy"/> </node>
what is aimed: I am trying to filter the records based on the attributes that i find in the dataset, <tag k="name" v="blahblahblah"/> field.
I have been looking around and the classical way to do it its easy, however because of my poor skills in doing anything Is it possible to apply xml filtering for tags that look like this <tag k="dispensing" v="yes"/> ones that do not have the <tag></tag>
.....format. Also how can i filter the records when each child of the node has different attributes,like v, <tag k="name">Pharmacie in Paris-downtown - Rapée-metro-station</tag>
i have had a quick look at the PHP-SimpleXML and the XPath-approach: it may help me in achieving what is aimed.
but at the moment - i think i got stuck.
i greatly appreciated hints and some ideas that may fit her.
have a great day. - yours dil_bert Edited December 1, 2019 by dil_bert <league name="italy" id="1"> <match name="AC Milan v Palermo" time="15/02/2011 20:45"> <bettype name="Versus (with Draw)"> <bet outcome_name="AC Milan" odd="1.87"> <bet outcome_name="X" odd="3.40"> <bet outcome_name="Palermo" odd="4.00"> </bettype> </match> <match name="Juventus v Torino" time="15/02/2011 20:45"> <bettype name="Versus (with Draw)"> <bet outcome_name="Juventus" odd="2.00"> <bet outcome_name="X" odd="3.00"> <bet outcome_name="Torino" odd="2.00"> </bettype> <bettype name="Half Time"> <bet outcome_name="1" odd="2.40"> <bet outcome_name="X" odd="3.40"> <bet outcome_name="2" odd="1.40"> </bettype> </match> </league> <league name="Spain" id="2"> <match name="Barcelona v Real Madrid" time="15/03/2011 21:45"> <bettype name="Versus (with Draw)"> <bet outcome_name="Barcelona" odd="1.87"> <bet outcome_name="X" odd="3.40"> <bet outcome_name="Real Madrid" odd="4.00"> </bettype> </match> </league> im trying to parse attributes of above xml with using xpath functions. what i want to output is this: ( <bettype name="Half Time"> this will be ignored ) Italy AC Milan v Palermo 1.87 3.40 4.00 Juventus v Torino 2.00 3.00 2.00 spain Barcelona v Real Madrid 1.87 3.40 4.00 im trying do this with below xpath codes $xml=simplexml_load_file('http://xml.gamebookers.com/sports/football.xml_attr.xml'); $league=$xml->xpath("//league"); $matches=$xml->xpath('//bettype[@name="Versus (with Draw)"]/..'); $odds=$xml->xpath('//match/bettype[@name="Versus (with Draw)"]/bet'); i searched and tried lots of tutorials. i need help in writing the correct code thanks I want to run a php file which contains a long process and takes time. If I simply run the file on browsers, the process will be interrupted by any perturbation in the internet connection or closing the browser. How I can force the php process to continue running without connection to my local PC (server side controlling)? I am looking for something like what cron job does (but only one time, not periodically). I am using MAMP on my MacBook and don't understand why MAMP and my code in NetBeans will not run if I am offline? If I am trying to execute PHP files locally on my laptop, why should MAMP or Apache of NetBEans care if I do not have an Internet connection?! I wanted to show someone something at work where there is no Internet access, but that won't work as it currently stands. TomTees ok. this may make now sense at all, I will try to explain as best as I can. anyways, what I need is for a mysql query to run that will pull all data from table payment between certain days. Then all the rows will show, but I want only one row with each cart_id(there will be multiple) to show. After it does that, in a new column on a table, all the payment types with the amount will show with a slash between each one. I want all of this in a table generated from a php loop. maybe a visual represenation will be better. I hope the picture I attached makes a bit more sense. Hi, I have a PHP script which I want to run from a different IP on my server. Example of what I want to do: My main ip: 4.5.6.7 Have several IP's on server. I want my PHP script (running from CLI) to use cURL with another IP, ay 4.5.6.9 Is this possible to do? Help would be greatly appreciated! |