0
<?php

include "../music/php/logic/core.php";
include "../music/php/logic/settings.php";
include "../music/php/logic/music.php";
$top = "At world's end";


// create doctype
$dom = new DOMDocument("1.0");

header("Content-Type: text/xml");

?>

<music>
<?php $_xml = "<title>".$top."</title>";
echo $_xml; ?>
</music>

I'm using this code to generate a dynamic XML document. The file is saved as PHP. My problem is that I can't echo php variables into the xml. However I can echo "literal" type text. I can't see anything wrong with my approach, it just doesn't work!

I'm pretty new to XML so I've probably missed something glaringly simple.

I've also tried lines like:

<title><?php echo $top; ?></title>
3
  • What output do you get with the first block of code in your question? Commented Mar 5, 2010 at 9:19
  • <title><?php echo $top; ?></title> should work fine, but you will need to escape the string before you echo it or you've got problems when it contains markup characters. (htmlspecialchars is fine for this in XML as well as HTML.) Alternatively use the DOM approach as posted by Gordon. At the moment you are creating a DOMDocument and then doing nothing with it at all. Commented Mar 5, 2010 at 9:30
  • I get <music> <title/> </music> Commented Mar 5, 2010 at 10:14

3 Answers 3

4

You don't use DOM this way. You use the DOM API to create the entire document:

$doc   = new DOMDocument();
$books = $doc->createElement( "books" );
$doc->appendChild( $books );
// ...

See:


A more verbose example (generating XHTML with DOM)

// Create head element
$head = $document->createElement('head');
$metahttp = $document->createElement('meta');
$metahttp->setAttribute('http-equiv', 'Content-Type');
$metahttp->setAttribute('content', 'text/html; charset=utf-8');
$head->appendChild($metahttp);

See this tutorial on how to use DOM for XHTML. For reuse of code, you can write your own classes extending DOM classes to get configurable components.


If you don't want to use DOM or want to use plain text for generating the XML, just approach it like any other template, e.g.

<root>
    <albums>
        <album id="<?php echo $albumId; ?>">
            <title><?php echo $title; ?></title>
            ... other elements ...
        </album>
    </albums>
</root>
Sign up to request clarification or add additional context in comments.

6 Comments

is that the same syntex as Javascript?
Your question is a bit confusing to me. Nothing in this answer has anything to do with javascript? Are you talking about ->?
@YsoL8 Syntax? No. PHP's syntax is obviously different from that of JavaScript. If you are refering to the methods exposed by the DOM API though, then the answer is yes. DOM is a language agnostic W3C interface recommendation and implemented in many languages. See w3.org/DOM
I'm following the plain text approach and making progress. I'll comment when I've tried a couple of things.
Right, this is working now. Question: my includes aren't included. do you know why? last comment
|
1

You can store your XMl string in a .php file then render it to get final formatted XMl string. In many cases simpler than playing with XMl writers

File template.php

<root>
    <albums>
        <album id="<?php echo $data['albumId']; ?>">
            <title><?php echo $data['title']; ?></title>
            ... other elements ...
        </album>
    </albums>
</root>

Render

function render($template, array $data)
{
    ob_start();
    include $template;
    return ob_get_clean();
}

Comments

-1

I think it's echo($_xml);

1 Comment

echo() is not actually a function (it is a language construct), so you are not required to use parentheses with it. - de3.php.net/manual/en/function.echo.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.