Jump to content

PHP variables to XML?


helraizer

Recommended Posts

Hi folks,

 

I'm only just learning XML (easier than I thought) but I have this layout for it. I have a form and php code to put the username, message etc.. into a database. The Layout of XML version is a lot better, though.. Would it be possible to add the username, title and message to the XML so it'd be like:

 

<messages>
<message>
<from><?php echo $user; ?> </from> <!-- Since you can't add '<' character into the XML -->
<title></title>
<body></body>
</message>
</messages>

 

<?xml version="1.0" encoding="ISO-8859-1"?>
<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">

  <body style="font-family:Arial,helvetica,sans-serif;font-size:12pt; background-color:#FFFFFF;">
    <xsl:for-each select="messages/message">
      <div style="background-color:#ececec;width:480px;padding:10px;">
    <xsl:value-of select="body"/>
      </div>

      <div style="background: url(arrow.gif) 20px 0 no-repeat;padding-left:58px;paddingtop:1px;margin-bottom:2em;font-size:90%;color:#4A4A4A">
        Title: <xsl:value-of select="title"/>
        <span style="font-style:italic">
          (Posted by: <xsl:value-of select="from"/>)
        </span>
      </div>
    </xsl:for-each>
  </body>

</html>

 

Or whatever, so eachtime the username, title and message are added within those tags (new tags for each username/message)?

 

Hope that's clear enough, holler if it isn't.

 

Sam

Link to comment
Share on other sites

The SimpleXML extension provides a very simple and easily usable toolset to convert XML to an object that can be processed with normal property selectors and array iterators.

 

See Example 8 on http://www.php.net/manual/en/ref.simplexml.php

 

Example#8 Setting values

 

Data in SimpleXML doesn't have to be constant. The object allows for manipulation of all of its elements.

<?php
include 'example.php';
$xml = new SimpleXMLElement($xmlstr);

$xml->movie[0]->characters->character[0]->name = 'Miss Coder';

echo $xml->asXML();
?>

The above code will output a new XML document, just like the original, except that the new XML will change Ms. Coder to Miss Coder.

Link to comment
Share on other sites

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.