SoFunction
Updated on 2025-03-10

Share 4 ways to generate XML files in PHP

Generate the following XML string
Xml code
Copy the codeThe code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<article>
    <item>
        <title size="1">title1</title>
        <content>content1</content>
        <pubdate>2009-10-11</pubdate>
    </item>
    <item>
        <title size="1">title2</title>
        <content>content2</content>
        <pubdate>2009-11-11</pubdate>
    </item>
</article>

Method I. [Directly generate strings]
Use pure PHP code to generate a string and write this string to a file with XML suffix. This is the most primitive way to generate XML, but it works!
Copy the codeThe code is as follows:

<?PHP
$data_array = array(
    array(
    'title' => 'title1',
    'content' => 'content1',
        'pubdate' => '2009-10-11',
    ),
    array(
    'title' => 'title2',
    'content' => 'content2',
    'pubdate' => '2009-11-11',
    )
);
$title_size = 1;
$xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
$xml .= "<article>\n";
foreach ($data_array as $data) {
$xml .= create_item($data['title'], $title_size, $data['content'], $data['pubdate']);
}
$xml .= "</article>\n";
echo $xml;
// Create XML single item
function create_item($title_data, $title_size, $content_data, $pubdate_data)
{
    $item = "<item>\n";
    $item .= "<title size=\"" . $title_size . "\">" . $title_data . "</title>\n";
    $item .= "<content>" . $content_data . "</content>\n";
    $item .= " <pubdate>" . $pubdate_data . "</pubdate>\n";
    $item .= "</item>\n";
    return $item;
}
?>

Method 2: [DomDocument]
Use DomDocument to generate XML files, create nodes with createElement method, createTextNode method to create text content, add child nodes with appendChild method, create attributes with createAttribute method
Copy the codeThe code is as follows:

<?php
$data_array = array(
    array(
    'title' => 'title1',
    'content' => 'content1',
        'pubdate' => '2009-10-11',
    ),
    array(
    'title' => 'title2',
    'content' => 'content2',
    'pubdate' => '2009-11-11',
    )
);
//    Attribute array
$attribute_array = array(
    'title' => array(
    'size' => 1
    )
);
//  Create an XML document and set the XML version and encoding. .
$dom=new DomDocument('1.0', 'utf-8');
// Create a root node
$article = $dom->createElement('article');
$dom->appendchild($article);
foreach ($data_array as $data) {
    $item = $dom->createElement('item');
    $article->appendchild($item);
    create_item($dom, $item, $data, $attribute_array);
}
echo $dom->saveXML();
function create_item($dom, $item, $data, $attribute) {
    if (is_array($data)) {
        foreach ($data as $key => $val) {
// Create elements
            $$key = $dom->createElement($key);
            $item->appendchild($$key);
// Create element value
            $text = $dom->createTextNode($val);
            $$key->appendchild($text);
            if (isset($attribute[$key])) {
// If there are related properties in this field, you need to set it
                foreach ($attribute[$key] as $akey => $row) {
// Create attribute node
                    $$akey = $dom->createAttribute($akey);
                    $$key->appendchild($$akey);
// Create attribute value node
                    $aval = $dom->createTextNode($row);
                    $$akey->appendChild($aval);
                }
            }   //  end if
        }
    }   //  end if
}   //  end function
?>

Method 3: [XMLWriter]
Create an XML file using the XMLWriter class, this method works after PHP 5.1.2. In addition, it can output multiple coded XML, but the input can only be utf-8
Copy the codeThe code is as follows:

<?php
$data_array = array(
    array(
    'title' => 'title1',
    'content' => 'content1',
        'pubdate' => '2009-10-11',
    ),
    array(
    'title' => 'title2',
    'content' => 'content2',
    'pubdate' => '2009-11-11',
    )
);
//    Attribute array
$attribute_array = array(
    'title' => array(
    'size' => 1
    )
);
$xml = new XMLWriter();
$xml->openUri("php://output");
//  The output method can also be set to a certain xml file address and output directly into a file
$xml->setIndentString('  ');
$xml->setIndent(true);
$xml->startDocument('1.0', 'utf-8');
// Start creating files
//  root node
$xml->startElement('article');
foreach ($data_array as $data) {
    $xml->startElement('item');
    if (is_array($data)) {
        foreach ($data as $key => $row) {
          $xml->startElement($key);
          if (isset($attribute_array[$key]) && is_array($attribute_array[$key]))
          {
              foreach ($attribute_array[$key] as $akey => $aval) {
// Set attribute value
                    $xml->writeAttribute($akey, $aval);
                }
            }
$xml->text($row);   //  Set content
            $xml->endElement(); // $key
        }
    }
    $xml->endElement(); //  item
}
$xml->endElement(); //  article
$xml->endDocument();
$xml->flush();
?>

Method 4: 【SimpleXML】
Create XML documents using SimpleXML
Copy the codeThe code is as follows:

<?php
$data_array = array(
    array(
    'title' => 'title1',
    'content' => 'content1',
        'pubdate' => '2009-10-11',
    ),
    array(
    'title' => 'title2',
    'content' => 'content2',
    'pubdate' => '2009-11-11',
    )
);
//    Attribute array
$attribute_array = array(
    'title' => array(
    'size' => 1
    )
);
$string = <<<XML
<?xml version='1.0' encoding='utf-8'?>
<article>
</article>
XML;
$xml = simplexml_load_string($string);
foreach ($data_array as $data) {
    $item = $xml->addChild('item');
    if (is_array($data)) {
        foreach ($data as $key => $row) {
          $node = $item->addChild($key, $row);
          if (isset($attribute_array[$key]) && is_array($attribute_array[$key]))
            {
              foreach ($attribute_array[$key] as $akey => $aval) {
// Set attribute value
                  $node->addAttribute($akey, $aval);
            }
          }
        }
    }
}
echo $xml->asXML();
?>