Creating a simple XML document

This example shows how to use XMLWriter to create a XML document in memory.

Beispiel #1 Creating a simple XML document

<?php

$xw 
xmlwriter_open_memory();
xmlwriter_set_indent($xw1);
$res xmlwriter_set_indent_string($xw' ');

xmlwriter_start_document($xw'1.0''UTF-8');

// A first element
xmlwriter_start_element($xw'tag1');

// Attribute 'att1' for element 'tag1'
xmlwriter_start_attribute($xw'att1');
xmlwriter_text($xw'valueofatt1');
xmlwriter_end_attribute($xw);

xmlwriter_write_comment($xw'this is a comment.');

// Start a child element
xmlwriter_start_element($xw'tag11');
xmlwriter_text($xw'This is a sample text, ä');
xmlwriter_end_element($xw); // tag11

xmlwriter_end_element($xw); // tag1


// CDATA
xmlwriter_start_element($xw'testc');
xmlwriter_write_cdata($xw"This is cdata content");
xmlwriter_end_element($xw); // testc

xmlwriter_start_element($xw'testc');
xmlwriter_start_cdata($xw);
xmlwriter_text($xw"test cdata2");
xmlwriter_end_cdata($xw);
xmlwriter_end_element($xw); // testc

// A processing instruction
xmlwriter_start_pi($xw'php');
xmlwriter_text($xw'$foo=2;echo $foo;');
xmlwriter_end_pi($xw);

xmlwriter_end_document($xw);

echo 
xmlwriter_output_memory($xw);

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

<?xml version="1.0" encoding="UTF-8"?>
<tag1 att1="valueofatt1">
 <!--this is a comment.-->
 <tag11>This is a sample text, ä</tag11>
</tag1>
<testc><![CDATA[This is cdata content]]></testc>
<testc><![CDATA[test cdata2]]></testc>
<?php $foo=2;echo $foo;?>

Hier Kannst Du einen Kommentar verfassen


Bitte gib mindestens 10 Zeichen ein.
Wird geladen... Bitte warte.
* Pflichtangabe
Es sind noch keine Kommentare vorhanden.

Was genau bedeutet "Vibe Coding"? Ein tiefgehender Blick für Entwickler

In der Welt der Softwareentwicklung gibt es unzählige Wege, wie man an ein Projekt herangeht. Manche schwören auf strikte Planung, andere auf bewährte Algorithmen und wieder andere lassen sich von etwas ganz anderem leiten: ihrem Gefühl. ...

admin

Autor : admin
Kategorie: Software & Web-Development

PHP cURL-Tutorial: Verwendung von cURL zum Durchführen von HTTP-Anfragen

cURL ist eine leistungsstarke PHP-Erweiterung, die es Ihnen ermöglicht, mit verschiedenen Servern über verschiedene Protokolle wie HTTP, HTTPS, FTP und mehr zu kommunizieren. ...

TheMax

Autor : TheMax
Kategorie: PHP-Tutorials

Midjourney Tutorial - Anleitung für Anfänger

Über Midjourney, dem Tool zur Erstellung digitaler Bilder mithilfe von künstlicher Intelligenz, gibt es ein informatives Video mit dem Titel "Midjourney Tutorial auf Deutsch - Anleitung für Anfänger" ...

Mike94

Autor : Mike94
Kategorie: KI Tutorials

Tutorial veröffentlichen

Tutorial veröffentlichen

Teile Dein Wissen mit anderen Entwicklern weltweit

Du bist Profi in deinem Bereich und möchtest dein Wissen teilen, dann melde dich jetzt an und teile es mit unserer PHP-Community

mehr erfahren

Tutorial veröffentlichen

Best Practices for Handling User Login and Session Security in PHP

Wondering how to boost login security? I've found setting session.cookie_secure and session.cookie_httponly in php.ini is a great start. Also, usi ...

Geschrieben von Carinaeeman am 08.09.2025 09:36:12
Forum: PHP Developer Forum
Grid und Pos geht nicht ...

So, you want Slider 1 to span columns 2-5, and Slider 2 to span columns 2-4 and also column 6 in your grid? From my experience, you'll need to adj ...

Geschrieben von Carinaeeman am 08.09.2025 09:34:37
Forum: PHP Developer Forum
Best practices for PHP in 2025?

I’ve been working with PHP for a while, but I’m curious about the community’s thoughts: What do you think are the most important best pract ...

Geschrieben von helen9k am 08.09.2025 03:49:25
Forum: Ankündigungen
How do you keep up with PHP and Database updates?

Hello everyone, PHP and database systems (MySQL, PostgreSQL, etc.) are evolving so quickly. Sometimes it feels hard to stay updated with new vers ...

Geschrieben von helen9k am 06.09.2025 10:35:52
Forum: Ankündigungen