Page 1 of 1

send link from PHP to XML... Need Help Please!

Posted: Tue Jul 28, 2009 6:13 pm
by PQAlex
I am trying to setup an administrative page so that users may log in and upload a file that will replace an existing file and also replace an existing link on a different web page(Flash) that draws its content from an XML file.

The user selects a file to upload. Selects a directory to upload the file to($location). And selects a name for the file($newname).

Once the file is uploaded (via PHP) the PHP script also sends information to the XML file.

The information sent to the XML file is a link generated by the information entered: $urlroot/$location/$newname
Reference:
$urlroot = http://www.mydomainname.com
$location= the directory that they have selected
$newname= the new name that they have given the file

The problem I run into is that when PHP sends the information to XML it has problems because of the special characters…

This is the PHP code that I have to send to XML:

$xml = simplexml_load_file("links.xml");

$sxe = new SimpleXMLElement($xml->asXML());

$links = $sxe->addChild("$location");

$links->addChild(“link”,“<![CDATA[<a href='$urlroot /$location/$newname'>$linkname</a>]]>");

$sxe->asXML("links.xml");

This is what shows up in the XML file (it already had the “links” node and I’ve edited the layout to make it easier to read, it actually comes out all on one line):

<?xml version="1.0" encoding="utf-8"?>
<links>
<TestingDirectory>
<link>
<![CDATA[<a href='"http://www.mydomain.com/testing/Testing ... nk.html</a>]]>
</link>
</TestingDirectory>
</links>

As you can see the <'s and >'s don't appear...

Basically what I’m trying to figure out is how to have php send the link to xml in a new node called link inside of a new node determined by $location and then have Flash pull that information and create a link on the webpage.

I know how to load the XML into Flash just can't seem to figure out how to pass the link from PHP to XML properly...

Please help if you can...

Thanks,
Alex

Re: send link from PHP to XML... Need Help Please!

Posted: Tue Jul 28, 2009 6:30 pm
by requinix
PQAlex wrote:As you can see the <'s and >'s don't appear...
They aren't supposed to. If they did the XML would look like

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<links>
    <TestingDirectory>
        <link>
            <![CDATA[<a href='"http://www.mydomain.com/testing/TestingDirectory/newLink.html"'>newLink.html</a>]]>
        </link>
    </TestingDirectory>
</links>
That has a different meaning: in this, the <![CDATA is part of the XML markup. The way you called addChild told it that the CDATA was actual text you want to include - so it had to be escaped.

If you want the CDATA as part of the text then you're doing it right. If you are including it because you don't want the <a> to be considered part of the XML then drop the CDATA declaration.

Code: Select all

$links->addChild("link","<a href='$urlroot /$location/$newname'>$linkname</a>");

Re: send link from PHP to XML... Need Help Please!

Posted: Tue Jul 28, 2009 8:40 pm
by PQAlex
In order for Flash to load the line correctly as a link it has to be:

<?xml version="1.0" encoding="utf-8"?>
<links>
<TestingDirectory>
<link>
<![CDATA[<a href='"http://www.mydomain.com/testing/Testing ... nk.html</a>]]>
</link>
</TestingDirectory>
</links>

So yes I do want it as part of the text, I just can't figure out how to get it to come into XML like this. Any idea why it is swapping the <'s and >'s for &gt and &lt?

Re: send link from PHP to XML... Need Help Please!

Posted: Tue Jul 28, 2009 11:21 pm
by requinix
Unless Flash is doing something it shouldn't,

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<links>
<TestingDirectory>
<link>
<a href='"http://www.mydomain.com/testing/TestingDirectory/newLink.html"'>newLink.html</a>
</link>
</TestingDirectory>
</links>
will work just as well.