I am trying to parse an xml document I created in a php file and outputted using
echo $xmlMysql->saveXML();
using cURL I send the information over, but when I try and parse it through using the following code.
$xmlDoc = download_page($url);
$dom = new DomDocument();
$dom->load($xmlDoc);
echo $dom->saveXML();
I get this error message,
<b>Warning</b>: I/O warning : failed to load external entity
^
any help with this would be much appreciated
if $xmlDoc is a string of XML that you're getting from an HTTP request, try using the loadXML method instead of just load method of your DomDocument object.
You can do
$dom = new DomDocument()
$dom->resolveExternals = false;
//...
to prevent external entities from being resolved. Of course, you may want to investigate which external entities are not being read. See also libxml_disable_entity_loader.
Try the following code:
$dom = dom_import_simplexml(simplexml_load_string($response))->ownerDocument;
$dom->formatOutput = true;
echo '<PRE style="color:#000066;padding:10px;text-align:left">',htmlspecialchars($dom->saveXML()),'</PRE>';
Related
I wrote this function to parse through html source code, but for some reason it does not work for feedburner feeds. Any ideas?
$dom = new DOMDocument();
$dom->loadHTMLFile('http://www.killington.com/winter/mountain/conditions');
$xml = simplexml_import_dom($dom);
$snow = $xml->xpath('//td');
What I really need to do is simply get the data from the page.
Not sure what the problem is other than the fact that this isnt a feed its a webpage. That said since youre using dom document theres no reason to bother with simplexml and that may be where the problem is coming in...
$dom = new DOMDocument();
$dom->loadHTMLFile('http://www.killington.com/winter/mountain/conditions');
$xpath = new DOMXPath($dom);
$snow = $xpath->query('//td');
First of all, you must open the feed page (the xml one, for example) and check which kind of feed it is:
<rss xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">
Then, you take a look at something like this good tutorial: http://net.tutsplus.com/articles/news/how-to-read-an-rss-feed-with-php-screencast/ and you're almost done :)
Yes, I realize that you can use javascript/jQuery to do this, but I want to use PHP (it's more a learning thing). I can't install queryPath or phpQuery since this is on a client's webhost.
Basically I'm modifying
function getElementById($id) {
$xpath = new DOMXPath($this->domDocument);
return $xpath->query("//*[#id='$id']")->item(0);
}
to use, but
Fatal error: Using $this when not in object context in blahblahblah on line #
get thrown and $this is undefined.
Basically what I'm trying to do is get the body id value of the same page the PHP is on.
Any ideas?
It looks like he is trying to just make a function to do some of this xpath stuff easier.
Something like
<?php
function getElementById($id,$url){
$html = new DOMDocument();
$html->loadHtmlFile($url); //Pull the contents at a URL, or file name
$xpath = new DOMXPath($html); // So we can use XPath...
return($xpath->query("//*[#id='$id']")->item(0)); // Return the first item in element matching our id.
}
?>
I didn't test this, but it looks about right.
I'm trying to create XML record. I started with this:
$doc = new DomDocument('1.0', 'UTF-8');
Now I should add this line:
<!DOCTYPE page SYSTEM "http://gv.ca/dtd/character-entities.dtd">
How do I do this?
Chris
Assuming it's PHP, you could consider using DOMImplementation::createDocumentType and DOMImplementation::createDocument. See here
I need to store the XML that i get it from Google Analytics. Its format is XML file. I need to create the script ( PHP ) that will read XML file from Google Analytics and store in my server with user defined name. I tried like that
<?php
$dom = new DOMDocument();
$dom->load('https://www.google.com/analytics/reporting/export?fmt=1&id=346044461&pdr=20100611-20100711&cmp=average&rpt=DashboardReport');
$dom->save('books3.xml');
?>
Can you help me
you're not assigning the result of load to anything you can save afterwards. and that is assuming you created a function load.
you'd need something more along the lines of
<?php
$remoteUri = 'https://www.google.com/analytics/reporting/export?...';
$doc = new DOMDocument();
$doc->loadXML(file_get_contents($remoteUri));
$xml = $doc->saveXML($doc->documentElement);
file_put_contents($yourLocalFilePath, $xml);
or if you just want a completely verbatim copy locally:
<?php
$remoteUri = ...
file_put_contents($yourLocalFilePath, file_get_contents($remoteUri));
the second, simpler version doesn't attempt to parse any xml and will therefore not have any clue if something is wrong with the recieved document.
depending on your server, you might have to resort to more complex methods of getting the file if url wrappers for fopen aren't enabled, or if your google endpoint wants to use cookies etc. for example.
I have an url return an XML page result. When I use this command:
print_r(file($url));
Its done, but when I use command:
$doc = load($url);
after that I :
print_r($doc);
it out. Its print_r out nothing. I'm quite new in work with XML in PHP someone give advise, please!
Thank you for your attention!
I am not really sure what you trying to do but for parsing an xml file in PHP there two main ways: DOM
$doc = new DOMDocument();
$doc->loadXML(file_get_contents($url));
SimpleXML
$xml = new SimpleXMLElement(file_get_contents($xmlstr));
file_get_contents Reads entire file into a string
#deceze and RageZ:
I'm using load() to get its attribute like this
$url = 'web address return an XML result';
$xml = load($url);
$node1 = $xml->getElmentsByTagName('tagname');
$value = $node1->getAttribute('attribute1');
But I have an error $xml is not an object and I check out by print_r and I get nothing but with print_r(file($url)) its print out an array as I expect!
#Franz: May be I get an error tag in XML file but I could not fixed this just work with the result!
You could also unserialize the xml into a php array and use print_r(array). Take a look here: http://articles.sitepoint.com/article/xml-php-pear-xml_serializer/3#
You will need a PEAR package for this