Asked  7 Months ago    Answers:  5   Viewed   34 times

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 :-)



Sure you can. Eg.

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


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

Have fun.

Wednesday, March 31, 2021
answered 7 Months ago


Since version 2.7.3 libxml limits the maximum size of a single text node to 10MB. The limit can be removed with a new option, XML_PARSE_HUGE. PHP has no way to specify this option to libxml.

So I imagine this flag is the way that PHP now has to specify this option.

Wednesday, March 31, 2021
answered 7 Months ago

simplexml_load_string() (as the name suggest) load xml from a string and returns an object of SimepleXMLElement. There is no difference between this and using just the usual constructor of the class.


SimpleXML::__construct() has an additional parameter (the third one) bool $data_is_url = false. If this is true the behavior is the same as simplexml_load_file() (in conjunction with the common stream wrappers). Thus both simplexml_load_*()-functions cover the same functionality as SimpleXML::__construct().

Additional the functions simplexml_load_*() has a second parameter string $class_name = "SimpleXMLElement" to specify the class of the object to get returned. Thats not a specific feature of the functions, because you can "use" something very similar with usual instanciation too

class MyXML extends SimpleXMLElement {}
$a = new MyXML($xml);
$b = simplexml_load_string($xml, 'MyXML');

A difference between the OOP- and the procedural approach is, that the functions return false on error, but the constructor throws an Exception.

Saturday, May 29, 2021
answered 5 Months ago

Set the content type to be XML, so that the browser will recognise it as XML.

header( "content-type: application/xml; charset=ISO-8859-15" );

In your code Change it to:

// Set the content type to be XML, so that the browser will   recognise it as XML.
header( "content-type: application/xml; charset=ISO-8859-15" );

// "Create" the document.
$doc = new DOMDocument( "1.0", "ISO-8859-15" );

+++I think you can do something like this

   foreach ( $row as $fieldname => $fieldvalue){
        <fieldname><?=fieldname; ?></fieldname>
        <fieldvalue><?=$fieldvalue; ?></fieldvalue>
Saturday, May 29, 2021
answered 5 Months ago

You need to create an XmlWriter and call its WriteNode method.

For example:

using (conn)
using (SqlCommand dataCmd = new SqlCommand(sqlText, Conn)) {
    dataCmd.CommandTimeout = 60000;

    using (XmlReader dataReader = dataCmd.ExecuteXmlReader())
    using (XmlWriter writer = XmlWriter.Create(File.OpenWrite(...)) {
        writer.WriteNode(dataReader, true);
Friday, July 30, 2021
srinivas p
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :