simzam Posted December 16, 2010 Share Posted December 16, 2010 Hello i just spent my hour finding way to get XML attribute using PHP via URL i wanted to know is it possible or not like http://myurl.com/index.php?$xml=link1 link1 in my XML is some text can we get attribute like this via using URL on address bar <?xml version="1.0" encoding="ISO-8859-1"?> <note> <link1>www.2advanced.com</link1> <link2>www.hotmail.com</link2> <link3>www.gmail.com</link3> <link4>www.twitter.com</link4> </note> Quote Link to comment https://forums.phpfreaks.com/topic/221854-how-to-get-xml-arrtributes-using-php-through-url-address/ Share on other sites More sharing options...
johnny86 Posted December 16, 2010 Share Posted December 16, 2010 Use PHPs DOMDocument: <?php $xml = new DOMDocument(); $xml->loadXML("your/xml/file.xml"); $nodes = $xml->getElementsByTagName($_GET['xml']); foreach($nodes as $node) { echo $node->textContents . "<br><br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/221854-how-to-get-xml-arrtributes-using-php-through-url-address/#findComment-1148047 Share on other sites More sharing options...
simzam Posted December 16, 2010 Author Share Posted December 16, 2010 error Warning: DOMDocument::loadXML() [domdocument.loadxml]: Start tag expected, '<' not found in Entity, line: 1 in C:\Apache-htdocs\example1.php on line 3 Quote Link to comment https://forums.phpfreaks.com/topic/221854-how-to-get-xml-arrtributes-using-php-through-url-address/#findComment-1148056 Share on other sites More sharing options...
johnny86 Posted December 16, 2010 Share Posted December 16, 2010 Actually you need to insert xml string for the loadxml function so do something like loadxml(file_get_contents("your/xml/file.xml")) Quote Link to comment https://forums.phpfreaks.com/topic/221854-how-to-get-xml-arrtributes-using-php-through-url-address/#findComment-1148076 Share on other sites More sharing options...
simzam Posted December 16, 2010 Author Share Posted December 16, 2010 when i remove this tag <?xml version="1.0" encoding="ISO-8859-1"?> it loads XML but not showing any results http://myurl.com/index.php?$xml=link1 when i try to access via URL nothing happens not getting any results from my XML as mention in first post Quote Link to comment https://forums.phpfreaks.com/topic/221854-how-to-get-xml-arrtributes-using-php-through-url-address/#findComment-1148101 Share on other sites More sharing options...
johnny86 Posted December 16, 2010 Share Posted December 16, 2010 Your link is wrong: http://myurl.com/index.php?$xml=link1 It should just be: http://myurl.com/index.php?xml=link1 No dollar sign! Quote Link to comment https://forums.phpfreaks.com/topic/221854-how-to-get-xml-arrtributes-using-php-through-url-address/#findComment-1148104 Share on other sites More sharing options...
simzam Posted December 16, 2010 Author Share Posted December 16, 2010 http://myurl.com/index.php?xml=link1 still it loads blank page my XML: <note> <link1>www.2advanced.com</link1> <link2>www.hotmail.com</link2> <link3>www.gmail.com</link3> <link4>www.twitter.com</link4> </note> my php : <?php $xml = new DOMDocument(); $xml->loadXML(file_get_contents('link.xml')); $nodes = $xml->getElementsByTagName($_GET['xml']); foreach($nodes as $node) { echo $node->textContents . "<br><br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/221854-how-to-get-xml-arrtributes-using-php-through-url-address/#findComment-1148110 Share on other sites More sharing options...
salathe Posted December 16, 2010 Share Posted December 16, 2010 There are three problems, one is stopping your script from being useful and another is just a choice. The third is a general tip. Firstly, the correct property to use is $node->textContent. Next, you can (and probably should) use $xml->load("link.xml"); and finally please learn to enable, display and understand error messages: use error_reporting(-1) while you're writing code and take note of the helpful messages. Quote Link to comment https://forums.phpfreaks.com/topic/221854-how-to-get-xml-arrtributes-using-php-through-url-address/#findComment-1148112 Share on other sites More sharing options...
simzam Posted December 16, 2010 Author Share Posted December 16, 2010 problem solved ! Thanks a lot johnny86 and salathe for your time third tip appreciated ! Quote Link to comment https://forums.phpfreaks.com/topic/221854-how-to-get-xml-arrtributes-using-php-through-url-address/#findComment-1148119 Share on other sites More sharing options...
simzam Posted December 16, 2010 Author Share Posted December 16, 2010 As every things working absolute fine I'm using error_reporting(-1) above my PHP code and getting this error how to get rid of it Notice: Undefined index: xml in C:\Apache-htdocs\example1.php on line 5 Quote Link to comment https://forums.phpfreaks.com/topic/221854-how-to-get-xml-arrtributes-using-php-through-url-address/#findComment-1148135 Share on other sites More sharing options...
BlueSkyIS Posted December 16, 2010 Share Posted December 16, 2010 make sure $_GET['xml'] is set or isn't empty before using it's value if (!empty($_GET['xml'])) { $xml = new DOMDocument(); $xml->loadXML(file_get_contents('link.xml')); $nodes = $xml->getElementsByTagName($_GET['xml']); foreach($nodes as $node) { echo $node->textContents . "<br><br>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/221854-how-to-get-xml-arrtributes-using-php-through-url-address/#findComment-1148150 Share on other sites More sharing options...
simzam Posted December 16, 2010 Author Share Posted December 16, 2010 thanks man gr8 info ! Quote Link to comment https://forums.phpfreaks.com/topic/221854-how-to-get-xml-arrtributes-using-php-through-url-address/#findComment-1148152 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.