Using SimpleXML to create an XML object from scratch





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







73















Is it possible to use PHP's SimpleXML functions to create an XML object from scratch? Looking through the function list, there's ways to import an existing XML string into an object that you can then manipulate, but if I just want to generate an XML object programmatically from scratch, what's the best way to do that?



I figured out that you can use simplexml_load_string() and pass in the root string that you want, and then you've got an object you can manipulate by adding children... although this seems like kind of a hack, since I have to actually hardcode some XML into the string before it can be loaded.



I've done it using the DOMDocument functions, although it's a little confusing because I'm not sure what the DOM has to do with creating a pure XML document... so maybe it's just badly named :-)










share|improve this question





























    73















    Is it possible to use PHP's SimpleXML functions to create an XML object from scratch? Looking through the function list, there's ways to import an existing XML string into an object that you can then manipulate, but if I just want to generate an XML object programmatically from scratch, what's the best way to do that?



    I figured out that you can use simplexml_load_string() and pass in the root string that you want, and then you've got an object you can manipulate by adding children... although this seems like kind of a hack, since I have to actually hardcode some XML into the string before it can be loaded.



    I've done it using the DOMDocument functions, although it's a little confusing because I'm not sure what the DOM has to do with creating a pure XML document... so maybe it's just badly named :-)










    share|improve this question

























      73












      73








      73


      26






      Is it possible to use PHP's SimpleXML functions to create an XML object from scratch? Looking through the function list, there's ways to import an existing XML string into an object that you can then manipulate, but if I just want to generate an XML object programmatically from scratch, what's the best way to do that?



      I figured out that you can use simplexml_load_string() and pass in the root string that you want, and then you've got an object you can manipulate by adding children... although this seems like kind of a hack, since I have to actually hardcode some XML into the string before it can be loaded.



      I've done it using the DOMDocument functions, although it's a little confusing because I'm not sure what the DOM has to do with creating a pure XML document... so maybe it's just badly named :-)










      share|improve this question














      Is it possible to use PHP's SimpleXML functions to create an XML object from scratch? Looking through the function list, there's ways to import an existing XML string into an object that you can then manipulate, but if I just want to generate an XML object programmatically from scratch, what's the best way to do that?



      I figured out that you can use simplexml_load_string() and pass in the root string that you want, and then you've got an object you can manipulate by adding children... although this seems like kind of a hack, since I have to actually hardcode some XML into the string before it can be loaded.



      I've done it using the DOMDocument functions, although it's a little confusing because I'm not sure what the DOM has to do with creating a pure XML document... so maybe it's just badly named :-)







      php xml simplexml






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Sep 27 '08 at 6:30









      dirtsidedirtside

      4,94293850




      4,94293850
























          3 Answers
          3






          active

          oldest

          votes


















          134














          Sure you can. Eg.



          <?php
          $newsXML = new SimpleXMLElement("<news></news>");
          $newsXML->addAttribute('newsPagePrefix', 'value goes here');
          $newsIntro = $newsXML->addChild('content');
          $newsIntro->addAttribute('type', 'latest');
          Header('Content-type: text/xml');
          echo $newsXML->asXML();
          ?>


          Output



          <?xml version="1.0"?>
          <news newsPagePrefix="value goes here">
          <content type="latest"/>
          </news>


          Have fun.






          share|improve this answer


























          • Between this and @Stefan's and @PhiLho's answers, this seems the most simple and straightforward. At the same time, I wonder if the performance is different. The other two smell like fast code.

            – Camilo Martin
            Mar 27 '11 at 5:42






          • 2





            How do i save this as a xml file in the server? to make it available to my actionscript code?

            – shababhsiddique
            Oct 6 '11 at 15:00











          • @dreamwerx Maybe I'm missing something why isn't your content tag closed with </content>?

            – Shackrock
            Dec 19 '12 at 21:41








          • 1





            @Shackrock:it is closed, look at the "/" before the ">" of <content type="latest"/>

            – Michel
            Dec 22 '12 at 14:34








          • 4





            How to add some text between content tags. Say <content> Wow it works! </content>

            – harishannam
            Mar 18 '14 at 19:42



















          21














          In PHP5, you should use the Document Object Model class instead.
          Example:



          $domDoc = new DOMDocument;
          $rootElt = $domDoc->createElement('root');
          $rootNode = $domDoc->appendChild($rootElt);

          $subElt = $domDoc->createElement('foo');
          $attr = $domDoc->createAttribute('ah');
          $attrVal = $domDoc->createTextNode('OK');
          $attr->appendChild($attrVal);
          $subElt->appendChild($attr);
          $subNode = $rootNode->appendChild($subElt);

          $textNode = $domDoc->createTextNode('Wow, it works!');
          $subNode->appendChild($textNode);

          echo htmlentities($domDoc->saveXML());





          share|improve this answer





















          • 1





            DOM API is so verbose! I'd love to be able to do something like $elem->append($doc->p('This is a paragraph')); Even appending text is a two line chore. I'd love something like $elem->append('Some text');

            – thomasrutter
            Jun 29 '16 at 2:09






          • 1





            In my opinion it depends a lot on what you are planing to do. If you want basic and fast functionality, SimpleXML is perfect! But anyways: the question was "Using SimpleXML to create an XML object from scratch" - so off-topic, sorry.

            – Stephan Weinhold
            Jan 24 '17 at 10:01



















          16














          Please see my answer here. As dreamwerx.myopenid.com points out, it is possible to do this with SimpleXML, but the DOM extension would be the better and more flexible way. Additionally there is a third way: using XMLWriter. It's much more simple to use than the DOM and therefore it's my preferred way of writing XML documents from scratch.



          $w=new XMLWriter();
          $w->openMemory();
          $w->startDocument('1.0','UTF-8');
          $w->startElement("root");
          $w->writeAttribute("ah", "OK");
          $w->text('Wow, it works!');
          $w->endElement();
          echo htmlentities($w->outputMemory(true));


          By the way: DOM stands for Document Object Model; this is the standardized API into XML documents.






          share|improve this answer


























          • XMLwriter is good if you are intending to output directly to XML code without any manipulation. Further manipulation (moving elements around, inserting elements) is not really possible.

            – thomasrutter
            Jun 29 '16 at 2:11











          • Updated links (I think) php.net/manual/en/intro.xmlwriter.php, php.net/manual/en/book.dom.php

            – chris85
            Aug 11 '16 at 15:21












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f143122%2fusing-simplexml-to-create-an-xml-object-from-scratch%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          134














          Sure you can. Eg.



          <?php
          $newsXML = new SimpleXMLElement("<news></news>");
          $newsXML->addAttribute('newsPagePrefix', 'value goes here');
          $newsIntro = $newsXML->addChild('content');
          $newsIntro->addAttribute('type', 'latest');
          Header('Content-type: text/xml');
          echo $newsXML->asXML();
          ?>


          Output



          <?xml version="1.0"?>
          <news newsPagePrefix="value goes here">
          <content type="latest"/>
          </news>


          Have fun.






          share|improve this answer


























          • Between this and @Stefan's and @PhiLho's answers, this seems the most simple and straightforward. At the same time, I wonder if the performance is different. The other two smell like fast code.

            – Camilo Martin
            Mar 27 '11 at 5:42






          • 2





            How do i save this as a xml file in the server? to make it available to my actionscript code?

            – shababhsiddique
            Oct 6 '11 at 15:00











          • @dreamwerx Maybe I'm missing something why isn't your content tag closed with </content>?

            – Shackrock
            Dec 19 '12 at 21:41








          • 1





            @Shackrock:it is closed, look at the "/" before the ">" of <content type="latest"/>

            – Michel
            Dec 22 '12 at 14:34








          • 4





            How to add some text between content tags. Say <content> Wow it works! </content>

            – harishannam
            Mar 18 '14 at 19:42
















          134














          Sure you can. Eg.



          <?php
          $newsXML = new SimpleXMLElement("<news></news>");
          $newsXML->addAttribute('newsPagePrefix', 'value goes here');
          $newsIntro = $newsXML->addChild('content');
          $newsIntro->addAttribute('type', 'latest');
          Header('Content-type: text/xml');
          echo $newsXML->asXML();
          ?>


          Output



          <?xml version="1.0"?>
          <news newsPagePrefix="value goes here">
          <content type="latest"/>
          </news>


          Have fun.






          share|improve this answer


























          • Between this and @Stefan's and @PhiLho's answers, this seems the most simple and straightforward. At the same time, I wonder if the performance is different. The other two smell like fast code.

            – Camilo Martin
            Mar 27 '11 at 5:42






          • 2





            How do i save this as a xml file in the server? to make it available to my actionscript code?

            – shababhsiddique
            Oct 6 '11 at 15:00











          • @dreamwerx Maybe I'm missing something why isn't your content tag closed with </content>?

            – Shackrock
            Dec 19 '12 at 21:41








          • 1





            @Shackrock:it is closed, look at the "/" before the ">" of <content type="latest"/>

            – Michel
            Dec 22 '12 at 14:34








          • 4





            How to add some text between content tags. Say <content> Wow it works! </content>

            – harishannam
            Mar 18 '14 at 19:42














          134












          134








          134







          Sure you can. Eg.



          <?php
          $newsXML = new SimpleXMLElement("<news></news>");
          $newsXML->addAttribute('newsPagePrefix', 'value goes here');
          $newsIntro = $newsXML->addChild('content');
          $newsIntro->addAttribute('type', 'latest');
          Header('Content-type: text/xml');
          echo $newsXML->asXML();
          ?>


          Output



          <?xml version="1.0"?>
          <news newsPagePrefix="value goes here">
          <content type="latest"/>
          </news>


          Have fun.






          share|improve this answer















          Sure you can. Eg.



          <?php
          $newsXML = new SimpleXMLElement("<news></news>");
          $newsXML->addAttribute('newsPagePrefix', 'value goes here');
          $newsIntro = $newsXML->addChild('content');
          $newsIntro->addAttribute('type', 'latest');
          Header('Content-type: text/xml');
          echo $newsXML->asXML();
          ?>


          Output



          <?xml version="1.0"?>
          <news newsPagePrefix="value goes here">
          <content type="latest"/>
          </news>


          Have fun.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 9 '14 at 7:54









          Joran Den Houting

          2,78431648




          2,78431648










          answered Sep 27 '08 at 7:42









          DreamWerxDreamWerx

          2,69711412




          2,69711412













          • Between this and @Stefan's and @PhiLho's answers, this seems the most simple and straightforward. At the same time, I wonder if the performance is different. The other two smell like fast code.

            – Camilo Martin
            Mar 27 '11 at 5:42






          • 2





            How do i save this as a xml file in the server? to make it available to my actionscript code?

            – shababhsiddique
            Oct 6 '11 at 15:00











          • @dreamwerx Maybe I'm missing something why isn't your content tag closed with </content>?

            – Shackrock
            Dec 19 '12 at 21:41








          • 1





            @Shackrock:it is closed, look at the "/" before the ">" of <content type="latest"/>

            – Michel
            Dec 22 '12 at 14:34








          • 4





            How to add some text between content tags. Say <content> Wow it works! </content>

            – harishannam
            Mar 18 '14 at 19:42



















          • Between this and @Stefan's and @PhiLho's answers, this seems the most simple and straightforward. At the same time, I wonder if the performance is different. The other two smell like fast code.

            – Camilo Martin
            Mar 27 '11 at 5:42






          • 2





            How do i save this as a xml file in the server? to make it available to my actionscript code?

            – shababhsiddique
            Oct 6 '11 at 15:00











          • @dreamwerx Maybe I'm missing something why isn't your content tag closed with </content>?

            – Shackrock
            Dec 19 '12 at 21:41








          • 1





            @Shackrock:it is closed, look at the "/" before the ">" of <content type="latest"/>

            – Michel
            Dec 22 '12 at 14:34








          • 4





            How to add some text between content tags. Say <content> Wow it works! </content>

            – harishannam
            Mar 18 '14 at 19:42

















          Between this and @Stefan's and @PhiLho's answers, this seems the most simple and straightforward. At the same time, I wonder if the performance is different. The other two smell like fast code.

          – Camilo Martin
          Mar 27 '11 at 5:42





          Between this and @Stefan's and @PhiLho's answers, this seems the most simple and straightforward. At the same time, I wonder if the performance is different. The other two smell like fast code.

          – Camilo Martin
          Mar 27 '11 at 5:42




          2




          2





          How do i save this as a xml file in the server? to make it available to my actionscript code?

          – shababhsiddique
          Oct 6 '11 at 15:00





          How do i save this as a xml file in the server? to make it available to my actionscript code?

          – shababhsiddique
          Oct 6 '11 at 15:00













          @dreamwerx Maybe I'm missing something why isn't your content tag closed with </content>?

          – Shackrock
          Dec 19 '12 at 21:41







          @dreamwerx Maybe I'm missing something why isn't your content tag closed with </content>?

          – Shackrock
          Dec 19 '12 at 21:41






          1




          1





          @Shackrock:it is closed, look at the "/" before the ">" of <content type="latest"/>

          – Michel
          Dec 22 '12 at 14:34







          @Shackrock:it is closed, look at the "/" before the ">" of <content type="latest"/>

          – Michel
          Dec 22 '12 at 14:34






          4




          4





          How to add some text between content tags. Say <content> Wow it works! </content>

          – harishannam
          Mar 18 '14 at 19:42





          How to add some text between content tags. Say <content> Wow it works! </content>

          – harishannam
          Mar 18 '14 at 19:42













          21














          In PHP5, you should use the Document Object Model class instead.
          Example:



          $domDoc = new DOMDocument;
          $rootElt = $domDoc->createElement('root');
          $rootNode = $domDoc->appendChild($rootElt);

          $subElt = $domDoc->createElement('foo');
          $attr = $domDoc->createAttribute('ah');
          $attrVal = $domDoc->createTextNode('OK');
          $attr->appendChild($attrVal);
          $subElt->appendChild($attr);
          $subNode = $rootNode->appendChild($subElt);

          $textNode = $domDoc->createTextNode('Wow, it works!');
          $subNode->appendChild($textNode);

          echo htmlentities($domDoc->saveXML());





          share|improve this answer





















          • 1





            DOM API is so verbose! I'd love to be able to do something like $elem->append($doc->p('This is a paragraph')); Even appending text is a two line chore. I'd love something like $elem->append('Some text');

            – thomasrutter
            Jun 29 '16 at 2:09






          • 1





            In my opinion it depends a lot on what you are planing to do. If you want basic and fast functionality, SimpleXML is perfect! But anyways: the question was "Using SimpleXML to create an XML object from scratch" - so off-topic, sorry.

            – Stephan Weinhold
            Jan 24 '17 at 10:01
















          21














          In PHP5, you should use the Document Object Model class instead.
          Example:



          $domDoc = new DOMDocument;
          $rootElt = $domDoc->createElement('root');
          $rootNode = $domDoc->appendChild($rootElt);

          $subElt = $domDoc->createElement('foo');
          $attr = $domDoc->createAttribute('ah');
          $attrVal = $domDoc->createTextNode('OK');
          $attr->appendChild($attrVal);
          $subElt->appendChild($attr);
          $subNode = $rootNode->appendChild($subElt);

          $textNode = $domDoc->createTextNode('Wow, it works!');
          $subNode->appendChild($textNode);

          echo htmlentities($domDoc->saveXML());





          share|improve this answer





















          • 1





            DOM API is so verbose! I'd love to be able to do something like $elem->append($doc->p('This is a paragraph')); Even appending text is a two line chore. I'd love something like $elem->append('Some text');

            – thomasrutter
            Jun 29 '16 at 2:09






          • 1





            In my opinion it depends a lot on what you are planing to do. If you want basic and fast functionality, SimpleXML is perfect! But anyways: the question was "Using SimpleXML to create an XML object from scratch" - so off-topic, sorry.

            – Stephan Weinhold
            Jan 24 '17 at 10:01














          21












          21








          21







          In PHP5, you should use the Document Object Model class instead.
          Example:



          $domDoc = new DOMDocument;
          $rootElt = $domDoc->createElement('root');
          $rootNode = $domDoc->appendChild($rootElt);

          $subElt = $domDoc->createElement('foo');
          $attr = $domDoc->createAttribute('ah');
          $attrVal = $domDoc->createTextNode('OK');
          $attr->appendChild($attrVal);
          $subElt->appendChild($attr);
          $subNode = $rootNode->appendChild($subElt);

          $textNode = $domDoc->createTextNode('Wow, it works!');
          $subNode->appendChild($textNode);

          echo htmlentities($domDoc->saveXML());





          share|improve this answer















          In PHP5, you should use the Document Object Model class instead.
          Example:



          $domDoc = new DOMDocument;
          $rootElt = $domDoc->createElement('root');
          $rootNode = $domDoc->appendChild($rootElt);

          $subElt = $domDoc->createElement('foo');
          $attr = $domDoc->createAttribute('ah');
          $attrVal = $domDoc->createTextNode('OK');
          $attr->appendChild($attrVal);
          $subElt->appendChild($attr);
          $subNode = $rootNode->appendChild($subElt);

          $textNode = $domDoc->createTextNode('Wow, it works!');
          $subNode->appendChild($textNode);

          echo htmlentities($domDoc->saveXML());






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Oct 4 '14 at 4:27









          JakeGould

          20.9k85176




          20.9k85176










          answered Sep 27 '08 at 8:50









          PhiLhoPhiLho

          35.3k481123




          35.3k481123








          • 1





            DOM API is so verbose! I'd love to be able to do something like $elem->append($doc->p('This is a paragraph')); Even appending text is a two line chore. I'd love something like $elem->append('Some text');

            – thomasrutter
            Jun 29 '16 at 2:09






          • 1





            In my opinion it depends a lot on what you are planing to do. If you want basic and fast functionality, SimpleXML is perfect! But anyways: the question was "Using SimpleXML to create an XML object from scratch" - so off-topic, sorry.

            – Stephan Weinhold
            Jan 24 '17 at 10:01














          • 1





            DOM API is so verbose! I'd love to be able to do something like $elem->append($doc->p('This is a paragraph')); Even appending text is a two line chore. I'd love something like $elem->append('Some text');

            – thomasrutter
            Jun 29 '16 at 2:09






          • 1





            In my opinion it depends a lot on what you are planing to do. If you want basic and fast functionality, SimpleXML is perfect! But anyways: the question was "Using SimpleXML to create an XML object from scratch" - so off-topic, sorry.

            – Stephan Weinhold
            Jan 24 '17 at 10:01








          1




          1





          DOM API is so verbose! I'd love to be able to do something like $elem->append($doc->p('This is a paragraph')); Even appending text is a two line chore. I'd love something like $elem->append('Some text');

          – thomasrutter
          Jun 29 '16 at 2:09





          DOM API is so verbose! I'd love to be able to do something like $elem->append($doc->p('This is a paragraph')); Even appending text is a two line chore. I'd love something like $elem->append('Some text');

          – thomasrutter
          Jun 29 '16 at 2:09




          1




          1





          In my opinion it depends a lot on what you are planing to do. If you want basic and fast functionality, SimpleXML is perfect! But anyways: the question was "Using SimpleXML to create an XML object from scratch" - so off-topic, sorry.

          – Stephan Weinhold
          Jan 24 '17 at 10:01





          In my opinion it depends a lot on what you are planing to do. If you want basic and fast functionality, SimpleXML is perfect! But anyways: the question was "Using SimpleXML to create an XML object from scratch" - so off-topic, sorry.

          – Stephan Weinhold
          Jan 24 '17 at 10:01











          16














          Please see my answer here. As dreamwerx.myopenid.com points out, it is possible to do this with SimpleXML, but the DOM extension would be the better and more flexible way. Additionally there is a third way: using XMLWriter. It's much more simple to use than the DOM and therefore it's my preferred way of writing XML documents from scratch.



          $w=new XMLWriter();
          $w->openMemory();
          $w->startDocument('1.0','UTF-8');
          $w->startElement("root");
          $w->writeAttribute("ah", "OK");
          $w->text('Wow, it works!');
          $w->endElement();
          echo htmlentities($w->outputMemory(true));


          By the way: DOM stands for Document Object Model; this is the standardized API into XML documents.






          share|improve this answer


























          • XMLwriter is good if you are intending to output directly to XML code without any manipulation. Further manipulation (moving elements around, inserting elements) is not really possible.

            – thomasrutter
            Jun 29 '16 at 2:11











          • Updated links (I think) php.net/manual/en/intro.xmlwriter.php, php.net/manual/en/book.dom.php

            – chris85
            Aug 11 '16 at 15:21
















          16














          Please see my answer here. As dreamwerx.myopenid.com points out, it is possible to do this with SimpleXML, but the DOM extension would be the better and more flexible way. Additionally there is a third way: using XMLWriter. It's much more simple to use than the DOM and therefore it's my preferred way of writing XML documents from scratch.



          $w=new XMLWriter();
          $w->openMemory();
          $w->startDocument('1.0','UTF-8');
          $w->startElement("root");
          $w->writeAttribute("ah", "OK");
          $w->text('Wow, it works!');
          $w->endElement();
          echo htmlentities($w->outputMemory(true));


          By the way: DOM stands for Document Object Model; this is the standardized API into XML documents.






          share|improve this answer


























          • XMLwriter is good if you are intending to output directly to XML code without any manipulation. Further manipulation (moving elements around, inserting elements) is not really possible.

            – thomasrutter
            Jun 29 '16 at 2:11











          • Updated links (I think) php.net/manual/en/intro.xmlwriter.php, php.net/manual/en/book.dom.php

            – chris85
            Aug 11 '16 at 15:21














          16












          16








          16







          Please see my answer here. As dreamwerx.myopenid.com points out, it is possible to do this with SimpleXML, but the DOM extension would be the better and more flexible way. Additionally there is a third way: using XMLWriter. It's much more simple to use than the DOM and therefore it's my preferred way of writing XML documents from scratch.



          $w=new XMLWriter();
          $w->openMemory();
          $w->startDocument('1.0','UTF-8');
          $w->startElement("root");
          $w->writeAttribute("ah", "OK");
          $w->text('Wow, it works!');
          $w->endElement();
          echo htmlentities($w->outputMemory(true));


          By the way: DOM stands for Document Object Model; this is the standardized API into XML documents.






          share|improve this answer















          Please see my answer here. As dreamwerx.myopenid.com points out, it is possible to do this with SimpleXML, but the DOM extension would be the better and more flexible way. Additionally there is a third way: using XMLWriter. It's much more simple to use than the DOM and therefore it's my preferred way of writing XML documents from scratch.



          $w=new XMLWriter();
          $w->openMemory();
          $w->startDocument('1.0','UTF-8');
          $w->startElement("root");
          $w->writeAttribute("ah", "OK");
          $w->text('Wow, it works!');
          $w->endElement();
          echo htmlentities($w->outputMemory(true));


          By the way: DOM stands for Document Object Model; this is the standardized API into XML documents.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited May 23 '17 at 12:18









          Community

          11




          11










          answered Sep 27 '08 at 10:01









          Stefan GehrigStefan Gehrig

          72.7k22132169




          72.7k22132169













          • XMLwriter is good if you are intending to output directly to XML code without any manipulation. Further manipulation (moving elements around, inserting elements) is not really possible.

            – thomasrutter
            Jun 29 '16 at 2:11











          • Updated links (I think) php.net/manual/en/intro.xmlwriter.php, php.net/manual/en/book.dom.php

            – chris85
            Aug 11 '16 at 15:21



















          • XMLwriter is good if you are intending to output directly to XML code without any manipulation. Further manipulation (moving elements around, inserting elements) is not really possible.

            – thomasrutter
            Jun 29 '16 at 2:11











          • Updated links (I think) php.net/manual/en/intro.xmlwriter.php, php.net/manual/en/book.dom.php

            – chris85
            Aug 11 '16 at 15:21

















          XMLwriter is good if you are intending to output directly to XML code without any manipulation. Further manipulation (moving elements around, inserting elements) is not really possible.

          – thomasrutter
          Jun 29 '16 at 2:11





          XMLwriter is good if you are intending to output directly to XML code without any manipulation. Further manipulation (moving elements around, inserting elements) is not really possible.

          – thomasrutter
          Jun 29 '16 at 2:11













          Updated links (I think) php.net/manual/en/intro.xmlwriter.php, php.net/manual/en/book.dom.php

          – chris85
          Aug 11 '16 at 15:21





          Updated links (I think) php.net/manual/en/intro.xmlwriter.php, php.net/manual/en/book.dom.php

          – chris85
          Aug 11 '16 at 15:21


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f143122%2fusing-simplexml-to-create-an-xml-object-from-scratch%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown