XML output - PHP vs JS vs Anything Else?

I am working on a Travel website that uses the XML API to retrieve data.

However, I am relatively new to XML and output it. I experimented using PHP to output a test XML file, but currently the farthest iv has only received the output of a few entries.

As these questions say, I need to know which technology will be better for this project. Below iv have included some points that should be taken into account.

  • The website will be a large and heavy traffic site (expedia / lastminute size).
  • My skill set is PHP (Intermediate / Highly Qualified) and Javascript (Medium / Highly Qualified).

The following is an example of XML that the API outputs:

<?xml version="1.0"?> <response method="###" success="Y"> <errors> </errors> <request> <auth password="test" username="test" /> <method action="###" sitename="###" /> </request> <results> <line id="6" logourl="###" name="Line 1" smalllogourl="###"> <ships> <ship id="16" name="Ship 1" /> <ship id="453" name="Ship 2" /> <ship id="468" name="Ship 3" /> <ship id="356" name="Ship 4" /> </ships> </line> <line id="63" logourl="###" name="Line 2" smalllogourl="###"> <ships> <ship id="492" name="Ship 1" /> <ship id="454" name="Ship 2" /> <ship id="455" name="Ship 3" /> <ship id="421" name="Ship 4" /> <ship id="401" name="Ship 5" /> <ship id="404" name="Ship 6" /> <ship id="405" name="Ship 7" /> <ship id="406" name="Ship 8" /> <ship id="407" name="Ship 9" /> <ship id="408" name="Ship 10" /> </ships> </line> <line id="41" logourl="###"> <ships> <ship id="229" name="Ship 1" /> <ship id="230" name="Ship 2" /> <ship id="231" name="Ship 3" /> <ship id="445" name="Ship 4" /> <ship id="570" name="Ship 5" /> <ship id="571" name="Ship 6" /> </ships> </line> </results> </response> 

If possible, when it is indicated which technology is best suited for this project, if you could provide some getting started guides, or any information would be greatly appreciated.

Thanks for taking the time to read this.

+1
source share
3 answers

XSLT works like a charm and is supported by PHP. To output your XML file, this XSLT script is required:

 <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <head> <title>Test</title> </head> <body> <h1>User: <xsl:value-of select="//request/auth/@username"/></h1> <xsl:apply-templates select="//results/line"/> </body> </html> </xsl:template> <xsl:template match="line"> <div> <h3>Line ID: <xsl:value-of select="@id"/></h3> <xsl:apply-templates select="./ships/ship"/> </div> </xsl:template> <xsl:template match="ship"> <div> <xsl:value-of select="@id"/> <xsl:text> - </xsl:text> <xsl:value-of select="@name"/> </div> </xsl:template> </xsl:stylesheet> 

To run a script for your file, use only 3 lines of PHP code:

 <?php $proc=new XsltProcessor; $proc->importStylesheet(DOMDocument::load("test.xsl")); //load script echo $proc->transformToXML(DOMDocument::load("test.xml")); //load your file ?> 

You can even try this conversion in your browser without adding PHP. <? xml-stylesheet type = "text / xsl" href = "test.xsl"? >
as the second line in your XML file and opening the XML file in Firefox or IE with the XSL file in the same folder.
PHP third line change in this
$ Process-> transformToUri (DOMDocument :: load ("test.xml"), "test.html");
will save the result as a static file.

Some helpful tips are offered here:
https://stackoverflow.com/questions/339930/any-good-xslt-tutorial-book-blog-site-online

0
source

I would suggest using the PHP SimpleXML extension. With SimpleXML, it is easy to parse XML data data and create formatted output from it.

http://php.net/manual/en/intro.simplexml.php

And some easy-to-understand examples:

http://php.net/manual/en/simplexml.examples-basic.php

0
source

I really like the XSLT solution offered in the last post. A great example of using XSLT.

0
source

All Articles