Four ways to generate XML files using PHP

Source: Internet
Author: User
Tags foreach addchild php code

Four ways to generate XML files using PHP

  

  

  

  

   Content1

2009-10-11

  

  

  

   Content2

2009-11-11

  

  

"Generate strings directly"

Method 1: Generate the string using pure PHP code and write this string to a file with XML suffix. This is the most original way to generate XML, but it works!

The PHP code is as follows:

  

$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 = " n";

$xml. = "

n ";

foreach ($data _array as $data) {

$xml. = Create_item ($data [' title '], $title _size, $data [' content '], $data [' pubdate ']);

}

$xml. = "

n ";

Echo $xml;

To create an XML item

function Create_item ($title _data, $title _size, $content _data, $pubdate _data)

{

$item = " n";

$item. = "n";

$item. = " ". $content _data. " n";

$item. = " ". $pubdate _data. " n";

$item. = "n";

return $item;

}

?>

"DOMDocument"

Method 2: Generate an XML file using DOMDocument

Create nodes using the CreateElement method,

Create text content using the createTextNode method,

Add child nodes using the AppendChild method,

Creating properties using the CreateAttribute method

The PHP code is as follows:

  

$data _array = Array (

Array

' title ' => ' Title1 ',

' Content ' => ' content1 ',

' pubdate ' => ' 2009-10-11 ',

),

Array

' title ' => ' Title2 ',

' Content ' => ' Content2 ',

' pubdate ' => ' 2009-11-11 ',

)

);

Property array

$attribute _array = Array (

' title ' => Array (

' Size ' => 1

)

);

Create an XML document and set up an XML version and encoding ...

$dom =new DOMDocument (' 1.0 ', ' utf-8 ');

To 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) {

Creating elements

$ $key = $dom->createelement ($key);

$item->appendchild ($ $key);

Create an element value

$text = $dom->createtextnode ($val);

$ $key->appendchild ($text);

if (Isset ($attribute [$key])) {

If this field has a related property that needs to be set

foreach ($attribute [$key] as $akey => $row) {

To create an attribute node

$ $akey = $dom->createattribute ($akey);

$ $key->appendchild ($ $akey);

Creating a property value node

$aval = $dom->createtextnode ($row);

$ $akey->appendchild ($aval);

}

}//End If

}

}//End If

}//End Function

?>

"XMLWriter"

Method 3: Create an XML file using the XmlWriter class

This method is valid after PHP 5.1.2

In addition, it can output a variety of encoded XML, but input can only be utf-8

The PHP code is as follows:

  

$data _array = Array (

Array

' title ' => ' Title1 ',

' Content ' => ' content1 ',

' pubdate ' => ' 2009-10-11 ',

),

Array

' title ' => ' Title2 ',

' Content ' => ' Content2 ',

' pubdate ' => ' 2009-11-11 ',

)

);

Property array

$attribute _array = Array (

' title ' => Array (

' Size ' => 1

)

);

$xml = new XMLWriter ();

$xml->openuri ("Php://output");

Output mode, can also be set to an XML file address, directly output to 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) {

Setting property values

$xml->writeattribute ($akey, $aval);

}

}

$xml->text ($row); Set content

$xml->endelement (); $key

}

}

$xml->endelement (); Item

}

$xml->endelement (); Article

$xml->enddocument ();

$xml->flush ();

?>

"SimpleXML"

Method 4: Create an XML document using SimpleXML

  

$data _array = Array (

Array

' title ' => ' Title1 ',

' Content ' => ' content1 ',

' pubdate ' => ' 2009-10-11 ',

),

Array

' title ' => ' Title2 ',

' Content ' => ' Content2 ',

' pubdate ' => ' 2009-11-11 ',

)

);

Property array

$attribute _array = Array (

' title ' => Array (

' Size ' => 1

)

);

$string = <<

  

  

  

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

Setting property values

$node->addattribute ($akey, $aval);

}

}

}

}

}

echo $xml->asxml ();

?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.