Jump to content

how to get xml arrtributes using PHP through URL address


Recommended Posts

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> 

 

 

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>";
}
?>

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

 

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>";
}
?>

 

 

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.

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>";
     }
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.