Using SimpleXML to create an XML object from scratch

Using SimpleXML to create an XML object from scratch

Asked on January 8, 2019 in XML.
Add Comment


  • 7 Answer(s)

    To create an XML object from scratch using SimpleXML try the code:

    <?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();
    ?>
    

    Result:

    <?xml version="1.0"?>
    <news newsPagePrefix="value goes here">
       <content type="latest"/>
    </news>
    
    Answered on January 8, 2019.
    Add Comment

         Use the Document Object Model class instead in PHP5 to create an XML object from scratch,

    $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());
    
    Answered on January 8, 2019.
    Add Comment

         It is possible to do this with SimpleXML, but the DOM extension will be better and flexible way. Also there is a third way: using XMLWriter.

    $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));
    
    Answered on January 8, 2019.
    Add Comment
    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 🙂

    Answered on February 28, 2019.
    Add Comment
    <?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();
    ?>
    Answered on February 28, 2019.
    Add Comment
    $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());
    Answered on February 28, 2019.
    Add Comment
    $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));
    Answered on February 28, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.