FAQ Database Discussion Community


PHP DOMDocument: how to incorporate a string of xml?

php,xml,domdocument
I am building up an xml file and need to include a segment of xml saved in a database (yeah, I wish that wasn't the case too). // parent element $parent = $dom->createElement('RecipeIngredients'); // the xml string I want to include $xmlStr = $row['ingredientSectionXml']; // load xml string into domDocument...

How to fetch custom or namespaced elements in QueryPath?

domdocument,querypath
I'm trying to fetch my custom elements in QueryPath library. But QueryPath does not work. Can anyone please help me how I can do it in QueryPath? See sample code below; <plugin:text path="filename" /> Moreover, I also checked the PhpSimpleDom library in detail. Although it works good for me. But...

Unable to load URL in DOMDocument

php,domdocument
I have the following URL : http://localhost:8000/PHPTrialProject/showPlace.php?lat=28.6139391&lng=77.20902120000005 I have extracted the query string from this and stored in two PHP variables : $latitude=$_GET['lat']; $longitude=$_GET['lng']; echo("latitude is : ".$latitude); echo("longitude is : ".$longitude); I then append them to my next URL and try to load it into DOMDocument. I tried two...

get all image source from a website in php

php,image,domdocument,fopen,src
What I want to do is let the user type a url which has images such as https://www.flickr.com/search/?text=arushad%20ahmed , and get all the image source in the 'src' attribute and display it. The following approach didn't work: $file = fopen("https://www.flickr.com/search/?text=arushad%20ahmed", "r"); $doc = new DOMDocument(); $doc->loadHTML($file); $image = $doc->getElementsByTagName('img'); foreach...

XML parser don't know whitespace length

xml,excel,vba,domdocument,ixmldomelement
I have the following XML sample. <?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:f="http://kbcfp.com/BoTech/Inferno/Fault" xmlns:base="http://kbcfp.com/BoTech/Inferno/Base" xmlns:h="http://kbcfp.com/BoTech/Inferno/Header" xmlns:ep="http://torstonetech.com/Inferno/FDE">...

HTML Parsing URL with DomDocument and DOMXPath - Get element by ID

php,html,xpath,domdocument
I've started to develop an script so i can parse an HTML dom elements. Here is what i have done already: <?PHP // to retrieve selected html data, try these DomXPath examples: $url = 'http://www.sportsdirect.com/nike-satire-mens-skate-shoes-242188?colcode=24218822'; libxml_use_internal_errors(true); $doc = new DOMDocument(); $doc->loadHTMLFile($url); $xpath = new DOMXpath($doc); $elements = $xpath->query("*/span[@id='ProductName']"); if (!is_null($elements))...

Why is getElementsByTagName() not working for me?

php,xml,xml-parsing,domdocument
I am trying to get info from the OMDb API. I found some code but it's not working right: $loc = 'http://www.omdbapi.com/?t='.$lookup_title.'&r=xml'; $dom = new DOMDocument(); $dom->load($loc); foreach ($dom->getElementsByTagName('movie') as $e) { $imdb = $e->getElementsByTagName('imdbID')->item(0)->textContent; $year = $e->getElementsByTagName('year')->item(0)->textContent; $plot = $e->getElementsByTagName('plot')->item(0)->textContent; $poster =...

Getting href-attributes using XPath in PHP

php,xpath,domdocument
I am new to PHP and trying to write a scrapper for a website. I am trying to get an element with class name categories. I have use $showPage = '<li class="categories">Categories<ul> <li class="cat-item cat-item-940"><a href="http://www.desitvbox.me/category/star-plus/amul-taste-of-india/" >Amul Taste of India</a> </li> <li class="cat-item cat-item-942"><a href="http://www.desitvbox.me/category/star-plus/dance-plus/" >Dance Plus</a> </li> <li class="cat-item...

PHP DOMDocument: what is the nicest way to safely add text to an element

php,xml,special-characters,domdocument
When adding a string that might contain troublesome characters (eg &, <, >), DOMDocument throws a warning, rather than sanitizing the string. I'm looking for a succinct way to make strings xml-safe - ideally something that leverages the DOMDocument library. I'm looking for something better than preg_replace or htmlspecialchars. I...

PHP DOM XML formatoutput adding white space in the first line

php,xml,domdocument
I've been struggling with this problem and I can't figure out why is there a white space in the first line of the xml file. I'm creating the XML as string, like so: $xml = '<?xml version="1.0" encoding="utf-8" standalone="no" ?>'; $xml .= '<AuditFile xmlns="urn:OECD:StandardAuditFile-Tax:PT_1.03_01">'; The following image represents the code...

Convert XML to Object in PHP and again convert that Object to XML? [duplicate]

php,xml,simplexml,domdocument
This question is an exact duplicate of: How to convert XML to OCI-Lob Object? 1 answer Lets say I have a XML as below - $xml = '<?xml version="1.0"?> <step number="9"> <s_name>test</s_name> <b_sel>12345</b_sel> <b_ind>7</b_ind> </step>'; I want this to be converted to object, but when I perform below steps,...

How to remove table row if condition met in table cell? - preg_replace or domdocument are welcome

php,html,table,preg-replace,domdocument
My HTML table. $table = <<< EOD <table> <tbody> <tr style="border:none;"> <td>1</td> <td>[[name_21]]</td> <td>[[charge_2]]</td> </tr> <tr style="border:1px solid #aaa;"> <td>2</td> <td>[[name_13]]</td> <td>[[charge_5]]</td> </tr> <tr style="border:1px solid #ccc;"> <td>3</td> <td>[[name_24]]</td> <td>[[charge_13]]</td> </tr> <tr style="border:1px solid #555;">...

Php cURL Web Scraping

php,html,xpath,web-scraping,domdocument
I want to scrape the Price of the mobile phone from the website url : http://www.flipkart.com/apple-iphone-5s/p/itmdv6f75dyxhmt4?pid=MOBDPPZZDX8WSPAT If you view the code, the price is placed in the following SPAN <div class="pricing line"> <div class="prices" itemprop="offers" itemscope="" itemtype="http://schema.org/Offer"> <div> <span class="selling-price omniture-field" data-omnifield="eVar48" data-eVar48="37500">Rs. 37,500</span> // Fetch this price </div> <span...

removeNode doesn't work correctly

php,domdocument
I want to remove all the anchor tags that starts with '/'. this is my code: $html = <<<HTML <ul> <li><a href="/foo/bar1">link1</li> <li><a href="/foo/bar2">link2</li> <li><a href="/foo/bar3">link3</li> </ul> HTML; $dom = new DOMDocument; @$dom->loadHTML($html); $tags = $dom->getElementsByTagName('a'); echo 'removed nodes:<br />'; foreach ($tags as $tag) { $href = $tag->getAttribute('href'); if($href[0] ==...

Getting value in rss feed using php

php,rss,domdocument
i want to get the value of a child in rss tag the code i'm using : $img = $item->getElementsByTagName('enclosure')->item(0)->firstChild->nodeValue; and the value i want to get is the url of the image, this is the RSS : <enclosure url="http://i.dailymail.co.uk/i/pix/2015/05/19/10/28D9A90800000578-0-image-m-14_1432029086109.jpg" type="image/jpeg" length="13451"/> what must i chang in my code to...

PHP DOM - Replacing TD tags in a THEAD to TH tags

php,html,domdocument
I'm trying to replace all TD tags within a THEAD to TH tags. I figured using the PHP DOM extension would be best. I'm fairly new at it so I apologise for my lack of knowledge. I did some searching and found how to replace tag names. However, I couldn't...

Fatal error: Call to a member function getElementsByTagName() WordPress 4.2.2 RSS Feed

php,xml,wordpress,rss,domdocument
I'm getting Fatal error: Call to a member function getElementsByTagName() now when I'm trying to pull in the RSS feed from my wordpress blog. Here's the code I'm using to get the RSS feed (which has worked up until recently): $xmlDoc = new DOMDocument(); @$xmlDoc->load('http://www.revolutionpersonaltraining.com.au/blog/feed/'); $x = $xmlDoc->getElementsByTagName('item'); for ($i=0;...

DOMDocument get element attribute

php,domdocument
I use DOMDocument object to get some data from this: <div class="prodImg"> <a href="#"><img src="some_image_src"/></a> </div> With this code: libxml_use_internal_errors(true); $homepage = file_get_contents('some_src'); $doc = new DomDocument; @$doc->loadHtml($homepage); $xpath = new DomXpath($doc); $div = $xpath->query('//*[@class="prodImg"]')->item(0); I get the whole div container but I want to get only the image src...

How to get the brother DOMNode from DOMElement php scraping?

php,web-scraping,domdocument
I'm working with PHP, and I'm try to scrapping a I little chunck of code, but in this moment I don't know how. The following is the simple structure that I can't scrap, the text inside of double quotes. <strong>Palabras: </strong> <br> " Biometría, Análisis de textura, Wavelets, Codificación predictiva,...

Should I initialize DOMdocument in the model or controller

oop,model-view-controller,structure,pip,domdocument
I have recently built a script that uses DOMdocument to generate an XML feed of posts from a CMS. I have this working but I would like to integrate this further as an MVC app. As a base structure I am using PIP. I like how bare bones it is...

Replace image tag by div with DOMDocument and return altered html

php,domdocument
In the following code, I'm trying to replace each image found in $content by some html. With $dom->saveHTML($image) I get the image html, but str_replace doesn't replace it. I don't know why. $content = '<div class="content"><img src="some-image.png"></div>'; $dom = new DOMDocument; $dom->loadHTML($content); $images = $dom->getElementsByTagName('img'); foreach ($images as $image) {...

php dom load form and create element inside

php,html,domdocument
What im trying to do is load a form and create elements inside it but i end up with something like this: <form id="form_54" accept-charset="utf-8"></form><input type="text" name="name"> The outpot i'm lookin for is: <form id="form_54" accept-charset="utf-8"><input type="text" name="name"></form> this is my function: public function input($name, $attributes = array(), $type =...

php spl_autoload_register() doesn't load a class

php,class,domdocument,autoload,spl-autoload-register
I have an index.php that require Test1 class trough spl_autoload_register(). In the Test1 class the Test2 class is required with the same autoload but this error occurred: Fatal error: DOMDocument::registerNodeClass(): Class Test2 does not exist in... I tried to see if the autoload work writing $test2 = new Test2(); and...

how can I get the value by tagnames in php?

php,domdocument,getelementsbytagname
could you give a suggestion to my code , please ? $string = "<li>CIs = <a href="http://localhost/itop/web/pages/UI.pdomhp?operation=details&class=FunctionalCI&id=49&c[menu]=ConfigManagementOverview" title="Functional CI::49">Sep Console04</a>, 42 49, Sep11, Sep Console04<br/><a href="http://localhost/itop/web/pages/UI.php?operation=details&class=FunctionalCI&id=50&c[menu]=ConfigManagementOverview" title="Functional CI::50">Sep Console05</a>, 42 50, Sep11, Sep...

Whats is the difference between these document ready types? When to use them?

javascript,jquery,dom,domdocument
I have been looking around a bit and haven't really been able to find something that says when is best to use these document ready types. The main one i use is $(document).ready(function() { because the syntax is clean and easy to read for me but is there an advantage...

PHP XML append to created file

php,xml,domdocument
I have the following XML documment: <list> <person> <name>Simple name</name> </person> </list> I try to read it, and basically create another "person" element. The output I want to achieve is: <list> <person> <name>Simple name</name> </person> <person> <name>Simple name again</name> </person> </list> Here is how I am doing it: $xml =...

PHP getElementsByTagName Scraping Links on Webpage Need to Determine if they contain Img elements

php,dom,domdocument,getelementsbytagname,getattribute
So I'm building a custom scraper for a project. I can currently scrape all of the links on a webpage, storing the HREF and anchor text in a database. However I am getting stuck when trying to determine if the anchor element contains and image element. Here is my code:...

PHP parsing won't find “span” tags

php,html,parsing,xpath,domdocument
I'm trying to find the span tags on a website similar to this: http://www.pointstreak.com/prostats/leagueschedule.html?leagueid=49&seasonid=14225. The tags I need are these: However, when I use code such as the following: $my_url = 'http://www.pointstreak.com/prostats/leagueschedule.html?leagueid=49&seasonid=14225'; $html = file_get_contents($my_url); $dom = new DOMDocument(); $dom->loadHTML($html); $xpath = new DOMXPath($dom); //Put your XPath Query here $my_xpath_query...

PHP version of pseudo after tag

php,html,domdocument
I am working on a project involving tens of thousands of files that I downloaded from the internet. The source of the pages (MO government) didn't program the pages too well. I am pulling certain elements from the pages to be put into another page to be referenced in my...

Create element if it does not exist in a PHP DOMDocument

php,xml,domdocument
I'm using PHP's DOMDocument to build a new xml file. I need to loop through an array of parent/children and add them to my xml. If the parent does not exist, I want to create it and then add a child. If it does exist, I want to add the...

DOMDocument - Load xml rss - failed to open stream

php,xml,domdocument
I want to get links from a rss url . This is my code : $doc = new DOMDocument(); $doc->load("http://www.alef.ir/rssdx.gmyefy,ggeltshmci.62ay2x.y.xml"); $arrFeeds = array(); foreach ($doc->getElementsByTagName('item') as $node) { $title = $node->getElementsByTagName('title')->item(0)->nodeValue; $title=strip_tags($title); $link=$node->getElementsByTagName('link')->item(0)->nodeValue; } I've used this code for several others URLs and all of them worked but on this...

PHP DomDocument's getElementsByTagName changing encoding

php,xml,character-encoding,domdocument
I'm trying to parse a XML and I am facing some problems with PHP DOMDocument::getElementsByTagName that I never had before. This is my code to get the XML $xmlToParse = new MXML('1,0','UTF-8'); $xmlToParse->loadXML(<myXML>); //print_r ($xmlToParse->saveXML());die(); When I use the print_r, this is the result (part of): <Hotel> <HotelId>10333</HotelId> <HotelName>Papillon Hotel</HotelName>...

Element 'foo': This element is not expected. Expected is ( {http://www.example.com}foo )

php,xml,validation,schema,domdocument
When trying to validate a PHP DOMDocument object against a schema by using schemaValidate method, the next warning is being generated: Warning: DOMDocument::schemaValidate(): Element 'foo': This element is not expected. Expected is ( {http://www.example.com}foo ). in X on line Y It only happens with elements that have been appended to...

Modify existing XML file with DomDocument PHP

php,xml,domdocument
I have an existing xml file of the following format: <root> <word> <label></label> <definition></definition> </word> ... </root> I'm taking in text through input boxes for the word and definition on the page and passing it to the php script via GET. The goal for the php script is to take...

Why the logic for array conversion and HTML parsing is not working in following scenario?

php,arrays,xpath,html-parsing,domdocument
I've an associative array titled $allFeeds (after executing print_r($allFeeds);) as follows : Note : The actual associative array $allFeeds is very large. For the understanding purpose I've only put one element from this large array. Array ( [0] => Array ( [feed_image] => Array ( [0] => <a href="http://52.1.47.143/photo/928/2_onclick_ok/userid_244/" class="...

Xpath Query Won't Return Results

php,xpath,domdocument,domxpath
I'm trying to return some results from an Xpath query but it won't select the elements correctly. I'm using the following code: public function getTrustPilotReviews($amount) { $trustPilotUrl = 'https://www.trustpilot.co.uk/review/purplegriffon.com'; $html5 = new HTML5; $document = $html5->loadHtml(file_get_contents($trustPilotUrl)); $document->validateOnParse = true; $xpath = new DOMXpath($document); $reviewsDomNodeList = $xpath->query('//div[@id="reviews-container"]//div[@itemprop="review"]'); $reviews = new Collection;...

PHP get svg tag from SVG file, and show it in HTML in DIV

php,svg,domdocument
I want to read an SVG file and get the SVG tag from this file (because I want to show svg in html e.g. <div><svg>...</svg></div> without the xml header). And show this svg tag in browser like HTML - print this SVG TAG like SVG image. Becouse now I'm gettong...

“Â ” character showing up instead of “ ”

php,dom,domdocument
I found this thread which describes my issue pretty well and this answer describes my issue exactly. The non-breaking space character is byte 0xA0 is ISO-8859-1; when encoded to UTF-8 it'd be 0xC2,0xA0, which, if you (incorrectly) view it as ISO-8859-1 comes out as "Â ". That includes a trailing...

What does the `document.write` do in this js snippet?

javascript,html,dom,browser,domdocument
The codes looks like this var addScript = function(path, server){ server = server || scriptConfig.server; document.write('<script type="text/javascript" src="' + server + path +'"></script>'); }; if (scriptConfig.autoLoadDependencies) { addScript("/javascripts/webtoolkit.base64.js"); addScript("/javascripts/swfobject.js"); addScript("/javascripts/jQuery/jquery-1.3.2.js"); addScript("/javascripts/jQuery/jquery-ui-1.7.1.custom.min.js"); addScript("/javascripts/underscore/underscore.js");...