I. Using the DOM to generate and read XML files
Example one:
Copy CodeThe code is as follows:
Creates XML string and XML document using the DOM
$dom = new DomDocument (' 1.0 ');
Add root-
$books = $dom->appendchild ($dom->createelement_x_x (' books '));
Add element to
$book = $books->appendchild ($dom->createelement_x_x (' book '));
Add <title>element to <book> <BR> $title = $book->appendchild ($dom->createelement_x_x (' title ')); <br>//add <title> text node element to <title> <BR> $title->appendchild ($dom, createTextNode (' Great American Novel ')); <br>//generate XML <BR> $dom->formatoutput = true; Set the Formatoutput attribute of DomDocument to True <br>//save XML as String or file <BR> $test 1 = $dom-&G T;savexml (); Put string in Test1 <BR> $dom, Save (' Test1.xml '); Save as file <BR>?> <BR> <BR> instance two: <br><span style= "Cursor:pointer" onclick= "Docopy (' Co de49077 ') ><U> copy code </U></span> code as follows: <BR> $AA = "111"; <BR> $xmlstr = <<<xml <br><?xml version= ' 1.0 '?> <BR><document> <BR>< title>{$AA}</title>
Joe
Jane
I know that ' s the answer--and what's the ' s the question?
XML;
$dom = new DomDocument;
$dom->loadxml ($XMLSTR);
$test 1 = $dom->savexml ();
$dom->save (' test1.xml ');
Example three:
Test1.xml:
Copy CodeThe code is as follows:
Jack Herrington
PHP Hacks
O ' Reilly
Jack Herrington
Podcasting Hacks
O ' Reilly
example.php:
Copy CodeThe code is 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 CodeThe code is as follows:
$xmlstr = <<
Great American Novel
Cliff
Really great guy
Lovely Woman
Matchless Beauty
Loyal Dog
Sleepy
Cliff meets lovely Woman. Loyal Dog sleeps, but wakes up to bark
At mailman.
4
9
XML;
Extracting 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
';
Break
Case ' bookclubs ':
Echo $success. ' Bookclub listings ';
Break
}
}
Modify text node content
$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 CodeThe code is 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 CodeThe code is as follows:
$sxe = simplexml_load_string (' Great American <br>novel ');
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 Importing the DOM:
Copy CodeThe code is as follows:
$dom = new DomDocument;
$dom->loadxml (' Great American <br>novel ');
if (! $dom) {
Echo ' Error while parsing the document ';
Exit
}
$s = Simplexml_import_dom ($dom);
Echo $s->book[0]->title; Great American Novel
?>
http://www.bkjia.com/PHPjc/326360.html www.bkjia.com true http://www.bkjia.com/PHPjc/326360.html techarticle I. Using the DOM to generate and read an XML file instance: The copy code code is as follows:? PHP//creates XML string and XML document using the DOM $dom = new DomDocument (' 1.0 '); Add Root-bo ...