Jump to content

Web service xml unserialize results


kirbkrawler

Recommended Posts

Hi,

 

I'm new to PHP and wondered if anyone could help...

 

I am consuming an asmx webservice using PHP code. (Later to make a widget for wordpress).

 

I can receive all the data, but it is in one very big xml file.

 

I want to be able to convert this data, so I can make it more readable.

 

Could really do with help as I don't have a clue how to convert it.

 

ANy ideas?

Link to comment
https://forums.phpfreaks.com/topic/221881-web-service-xml-unserialize-results/
Share on other sites

Hello,

You didn't mention how you want to convert it. I assume you want to show it in website as (X)HTML.

 

This tutorial might be useful to get an idea. You already have a xml file. So all you need to do is create stylesheet for it and transform it with PHP. Look into here: http://devzone.zend.com/article/1302

These XML and XSL files are taken form O'Reilly site:

 

This is example XML (story.xml):

<?xml version="1.0" encoding="utf-8" ?>
  
<news xmlns:news="http://slashdot.org/backslash.dtd">
  <story>
    <title>O'Reilly Publishes Programming PHP</title>
    <url>http://example.org/article.php?id=20020430/458566</url>
    <time>2002-04-30 09:04:23</time>
    <author>Rasmus and some others</author>
  </story>
  
  <story>
    <title>Transforming XML with PHP Simplified</title>
    <url>http://example.org/article.php?id=20020430/458566</url>
    <time>2002-04-30 09:04:23</time>
    <author>k.tatroe</author>
  </story>
</news>

 

This is example XSL (story.xsl): (XML Stylesheet)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output
  method="html"
  indent="yes"
  encoding="utf-8"
/>
  
<xsl:template match="/news"> 
  <html> 
    <head> 
      <title>Current Stories</title> 
    </head> 
    <body bgcolor="white" >
      <xsl:call-template name="stories"/>
    </body>
  </html>
</xsl:template> 
  
<xsl:template name="stories">
  <xsl:for-each select="story">
    <h1><xsl:value-of select="title" /></h1>
  
    <p>
      <xsl:value-of select="author"/> (<xsl:value-of select="time"/>)<br/>
      <xsl:value-of select="teaser"/>
      [ <a href="{url}">More</a> ]
    </p>
  
    <hr />
  </xsl:for-each>
</xsl:template>
  
</xsl:stylesheet> 

 

And here is the code to process it. This will actually transform the XML file to HTML according to the XSL file.

This is simplified example. But I chose to use the Object Oriented way instead of the tutorials procedural way.

<?php

$xml = new DOMDocument();
$xml->load("story.xml");

$xsl = new DOMDocument();
$xsl->load("story.xsl");

$transform = new XSLTProcessor();
$transform->importStylesheet($xsl);
echo $transform->transformToXml($xml);

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.