PHP Operation XML

Source: Internet
Author: User
Tags addchild

<?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>

"Generate string Directly"
Method 1: Generate the string using pure PHP code and write the string to a file that is suffixed with XML. This is the most primitive method of generating XML, but it works!
The PHP 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 an XML 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;
}

?>

"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,
To create an attribute using the CreateAttribute method
The PHP 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 ',
)
);

Property 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 ');

Creating the 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);

Creating element values
$text = $dom->createtextnode ($val);
$ $key->appendchild ($text);

if (Isset ($attribute [$key])) {
If there are related attributes in this field, you need to set
foreach ($attribute [$key] as $akey = = $row) {
To create an attribute node
$ $akey = $dom->createattribute ($akey);
$ $key->appendchild ($ $akey);

Creating attribute Value Nodes
$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 the input can only be utf-8
The PHP 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 ',
)
);

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, output directly into a file
$xml->setindentstring (");
$xml->setindent (TRUE);

$xml->startdocument (' 1.0 ', ' utf-8 ');
Start creating a file
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

<? Php
$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 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) {
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.