0

I want to create a XML File (1.0) via PHP but I have problems with using DOMDocument.

My code:

<?php
header('Content-Type:text/xml');
$xml = new DOMDocument('1.0', 'utf-8');
$root = $xml->createElement('root');
$root->setAttribute('id', '1234');
$root->setAttribute('date', '26.02.2018');
//info
$xml->appendChild($root);
$info = $xml->createElement('information');
$root->appendChild($info);
//updates
$updates = $xml->createElement('updates');
$info->appendChild($updates);
//updated
$updated= $xml->createElement('updated');
$updates->appendChild($updated);
//client
$client = $xml->createElement('client', 'exampleClient');
$updates->appendChild($updated);
//clientID
$clientID = $xml->createElement('clientID', '123456');
echo $xml->saveXML();
?>

Output:

<root id="1234" date="26.02.2018">
    <information>
        <updates>
            <updated/>
        </updates>
    </information>
</root>

As you can see, it doesn't display the createElement() Data like 'client' and 'exampleClient'.

My desired output:

<root id="1234" date="26.02.2018">
<information>
    <updates>
        <updated>
            <client>exampleClient</client>
            <clientID>123456</clientID>
        </updated>
    </updates>
</information>
</root>

Does anyone have any idea how I can solve the problem?

4
  • For your further development: Please avoid code like new DOMDocument and use namespaces instead of classnames like new Vendor/DOMDocument Commented Feb 16, 2018 at 9:46
  • You created the client and clientID elements but you didn't attach them to the XML document. Of course they don't show up in the output of $xml->saveXML(). Commented Feb 16, 2018 at 9:48
  • You have many unclosed questions, IMHO. Please have a look to meta.stackexchange.com/a/5235 . For all other anwsers and futur readers. Thank you! Commented Feb 16, 2018 at 9:53
  • @unherz thanks for the hint! @ axiac thank you, now it works Commented Feb 16, 2018 at 9:54

2 Answers 2

2

You're missing to append $client and $clientID into $updated:

$updated->appendChild($client);
$updated->appendChild($clientID);
echo $xml->saveXML();
Sign up to request clarification or add additional context in comments.

1 Comment

thank you, now it works correct! i will vote your answer up if i will have enough reputation.
0

You Can also use code to get your answer

$xml = new SimpleXMLElement('<root/>');

    $xml->addAttribute( "id", "123" );
    $xml->addAttribute("date", "26.02.2018" );
    $information = $xml->addChild('information');
    $updates = $information->addChild('updates');
    $updated = $updates->addChild('updated');
    $client = $updated->addChild('client','exampleClient');
    $clientID = $updated->addChild('clientID','123456');
Header('Content-type: text/xml');
print($xml->asXML());

Comments

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.