Several ways to write and read XML in PHP _php tips

Source: Internet
Author: User
Tags addchild
I. Using the DOM to generate and read XML files
Example one:
Copy Code code as follows:

<?php
Creates XML string and XML document using the DOM
$dom = new DOMDocument (' 1.0 ');
Add Root-<books>
$books = $dom->appendchild ($dom->createelement_x_x (' books '));
Add <book> element to <books>
$book = $books->appendchild ($dom->createelement_x_x (' book '));
Add <title> element to <book>
$title = $book->appendchild ($dom->createelement_x_x (' title '));
Add <title> text node element to <title>
$title->appendchild ($dom->createtextnode (' Great American Novel '));
Generate XML
$dom->formatoutput = true; Set the formatoutput of DOMDocument to True
Save XML As String or file
$test 1 = $dom->savexml (); Put string in Test1
$dom-> Save (' Test1.xml '); Save As File
?>

Example two:
Copy Code code as follows:

$AA = "111";
$xmlstr = <<<xml
<?xml version= ' 1.0 '?>
<document>
<title>{$aa}</title>
<from>Joe</from>
<to>Jane</to>
<body>
I know that ' s the answer--but what ' s question?
</body>
</document>
XML;
$dom = new DOMDocument;
$dom->loadxml ($XMLSTR);
$test 1 = $dom->savexml ();
$dom->save (' test1.xml ');


Example three:
Test1.xml:
Copy Code code as follows:

<?xml version= "1.0"?>
<books>
<book>
<author>jack herrington</author>
<title>php hacks</title>
<publisher>o ' reilly</publisher>
</book>
<book>
<author>jack herrington</author>
<title>podcasting hacks</title>
<publisher>o ' reilly</publisher>
</book>
</books>


example.php:
Copy Code code as follows:

$doc = new DOMDocument ();
$doc->load (' test1.xml ');
$books = $doc->getelementsbytagname ("book");
foreach ($books as $book) {
$authors = $book->getelementsbytagname ("author");
$author = $authors->item (0)->nodevalue;
$publishers = $book->getelementsbytagname ("publisher");
$publisher = $publishers->item (0)->nodevalue;
$titles = $book->getelementsbytagname ("title");
$title = $titles->item (0)->nodevalue;
echo "$title-$author-$publisher \ n";
}


Two. Using simple to generate and read XML files
Example one:
Copy Code code as follows:

?
$xmlstr = <<<xml
<?xml version= ' 1.0 ' standalone= ' yes '?>
<books>
<book>
<title>great American novel</title>
<characters>
<character>
<name>Cliff</name>
<desc>really Great guy</desc>
</character>
<character>
<name>lovely woman</name>
<desc>matchless beauty</desc>
</character>
<character>
<name>loyal dog</name>
<desc>sleepy</desc>
</character>
</characters>
<plot>
Cliff meets lovely Woman. Loyal Dog sleeps, but wakes up to bark
At mailman.
</plot>
<success type= ' bestseller ' >4</success>
<success type= ' bookclubs ' >9</success>
</book>
</books>
XML;

Extract node Content
$xml = new SimpleXMLElement ($XMLSTR);
foreach ($xml->book[0]->success as $success) {
Switch ((string) $success [' type ']) {//Get attributes as element indices
Case ' bestseller ':
Echo $success. ' Months on bestseller list<br> ';
Break
Case ' bookclubs ':
Echo $success. ' Bookclub listings ';
Break
}
}

Modify Text node contents
$xml = new SimpleXMLElement ($XMLSTR);
$xml->book[0]->characters->character[0]->name = ' big Cliff ';
echo $xml->asxml ();

Add a text node for a child element
$xml = new SimpleXMLElement ($XMLSTR);
$character = $xml->book[0]->characters->addchild (' character ');
$character->addchild (' name ', ' Yellow Cat ');
$character->addchild (' desc ', ' aloof ');
$success = $xml->book[0]->addchild (' Success ', ' 2 ');
$success->addattribute (' type ', ' reprints ');
echo $xml->asxml ();

?>


Example two:
Copy Code code as follows:

if (file_exists (' Test1.xml ')) {//Read XML file
$xml = simplexml_load_file (' Test1.xml ');
Var_dump (XML);
} else {
Exit (' Failed to open test1.xml. ');
}


Three. Dom and simple Interop
Dom Import SimpleXML:
Copy Code code as follows:

<?php
$sxe = simplexml_load_string (' <books><book><title>great American
Novel</title></book></books> ');
if ($sxe = = False) {
Echo ' Error while parsing the document ';
Exit
}
$dom _sxe = Dom_import_simplexml ($SXE);
if (! $dom _sxe) {
Echo ' Error while converting XML ';
Exit
}
$dom = new DOMDocument (' 1.0 ');
$dom _sxe = $dom->importnode ($dom _sxe, true);
$dom _sxe = $dom->appendchild ($dom _sxe);
$test 2 = $dom->savexml (); Put string in Test2
$dom-> Save (' Test2.xml '); Save As File
?>


SimpleXML Import DOM:
Copy Code code as follows:

<?php
$dom = new DOMDocument;
$dom->loadxml (' <books><book><title>great American
Novel</title></book> </books> ');
if (! $dom) {
echo ' Error while parsing the document ';
Exit;
}
$s = Simplexml_import_dom ($dom);
Echo $s->book[0]->title;//Great American novel
?>

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.