working with XML in PHP - php

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

Related

How to create a XML File From Server Response (PHP)

I have a response that I got from a server using PHP, it is returned in XML format. Unfortunately I cannot disclose the code that is in question since it is for a client, but here is a shortened version of it:
// server request code here…
$result = $soap>__doRequest($xmltosend,URL,$action,1); //the final part of the request process…
// $result is the returned xml that is to be converted into a separate file
Is there any way that I could take the returned XML and make a new file? If you have any questions I will be more than happy to answer them!
Thanks in advance!
Try it with DOMDocument:
$doc = new DOMDocument();
$doc->loadXML($result);
$doc->save("/tmp/your-document.xml");
You can find more on this in the php documentation

How can I load POST data into SimpleXML?

I need to post XML data from a textarea input to PHP so I can parse it and output a table.
I've tried a few methods and none seem to be working.
Currently I have this:
jQuery('#btnRegistrarXML').live('click', function(){
var xml;
if (jQuery('#txtRegXML').val() == ""){
AddMsg('You must paste XML from the excel export into the textarea.', 'error');
} else {
jQuery.post('registrar-xml-to-table.php', {xml:escape(jQuery('#txtRegXML').val())}, function(data){
jQuery('#regXMLasTable').empty();
jQuery('#regXMLasTable').append(data);
});
}
displayMsgs();
});
The PHP is:
$xmlraw = urldecode($_POST['xml']);
$dom = new DOMDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->loadXML($xmlraw);
$dom->formatOutput = TRUE;
$xmlstr = $dom->saveXml();
$xml = simplexml_load_string($xmlstr);
echo "<p>xmlraw:</p>";
echo $xmlraw;
echo "<p>xml:</p>";
echo $xml;
foreach ($xml->document as $doc) {
echo '<p class="alert alert-error">'.$doc->title.'</p>';
}
The first echo $xmlraw is working - it outputs the XML string all on one line - the post is sending the data through properly.
The second echo $xml doesn't output anything and the foreach doesn't output anything either - something is not working in the PHP
I've also tried loading $xmlraw directly into simplexml_load_string($xmlraw) but it doesn't work - I'm assuming because it's not well formed?
The XML I'm using to test is:
<?xml version="1.0"?>
<document>
<title>
Foobar
</title>
</document>
You can paste the XML into the textarea on this ( http://tsdexter.com/webservice-testing/programs-list.php ) page and then if you inspect element underneath xmlraw: you can see that the raw xml string was echoed - however, underneath xml: there is nothing and also the foreach doesn't output anything either.
Here's a jsFiddle so you can see the HTML/JS - it doesn't actually do anything though because it can't ajax to the PHP page - so I included the PHP version above to test. http://jsfiddle.net/tsdexter/EDqQB/
Any ideas?
A few questions, which may or may not constitute an answer, but needed more space than a comment.
Why are you running urldecode on the $_POST variable? PHP should be doing that for you, unless you are double-escaping it somewhere.
Why are you loading into DOM and then SimpleXML? I know you said you tried without, but they use the same XML parser underneath, so there is no way this will help you rather than introducing more confusion.
What does var_dump(simplexml_load_string($rawxml)) give you? My guess would be FALSE, meaning an error. In which case, check you have warnings turned on (error_reporting(E_ALL); ini_set('display_errors', '1');) and detailed XML error handling turned off (libxml_use_internal_errors(false);).
In case it does give you a SimpleXMLElement object, don't spend too long looking at the var_dump output. Try one of these debugging functions instead.

php: Getting the contents of a feedburner feed

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 :)

Can't Parse RSS XML with PHP

I have some RSS that I am trying to parse from this URL:
http://weather.yahooapis.com/forecastrss?w=12797541
But when I try to parse it using PHP doing this:
$yahoo_response = new SimpleXMLElement($yahoo_url , 0, true);
echo $yahoo_response->rss->channel->item->title;
echo $yahoo_response->rss->channel->item->description;
Nothing gets outputed. Does anyone know what I am doing wrong here? I just need the current forecast bit.
Thanks,
Alex
The root element is <rss>, which is represented by the SimpleXmlElement you loaded into $yahoo_response.
echo $yahoo_response->getName(); // rss
You are trying to do <rss><rss> when you should do:
echo $yahoo_response->channel->item->title;
echo $yahoo_response->channel->item->description;
I've found that RSS feeds are best parsed using a library.
I've had much success with magpieRSS.
But your immediate problem is that you are passing a URL to simplexml, instead of the xml itself.
try $xml = file_get_contents($yahoo_url); first.

parsing xml document using cURL

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>';

Categories