| The code is as follows: |
Copy code |
|
$ Xml_str = "<? Xml version = '1. 0'?> ";
$ Xml_str. = "<books> ";
$ Xml_str. = "<book> ";
$ Xml_str. = "<title> harry potter </title> ";
$ Xml_str. = "<author> j. k. rowling </author> ";
$ Xml_str. = "<publisher> warner bros. </publisher> ";
$ Xml_str. = "<price> 39.0 </price> ";
$ Xml_str. = "</book> ";
$ Xml_str. = "</books> ";
$ Dom = new domdocument;
$ Dom-> loadxml ($ xml_str );
Echo $ dom-> savexml ();
$ Dom-> save ("test. xml ");
// Load the xml document using domdocument
$ Dom = new domdocument;
$ Dom-> load ('17-1. XML ');
$ Root = $ dom-> documentelement; |
// Parse the xml document
| The code is as follows: |
Copy code |
|
$ Doc = new domdocument ();
$ Doc-> load ("17-1.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;
$ Prices = $ book-> getelementsbytagname ("price ");
$ Price = $ prices-> item (0)-> nodevalue;
Echo "$ title-$ author-$ publisher-$ price ";
Echo "<br/> ";
Echo "<br/> ";
} |
/*
17-1.xml
| The code is as follows: |
Copy code |
<? Xml version = "1.0" encoding = "gb2312"?>
<Books>
<Book>
<Title> learning php5 </title>
<Author> david </author>
<Publisher> white water press </publisher>
<Price> 29.90 </price>
</Book>
<Book>
<Title> learning xml </title>
<Author> jeffson </author>
<Publisher> white water press </publisher>
<Price> 50.79 </price>
</Book>
<Book>
<Title> using perl </title>
<Author> lucas </author>
<Publisher> white water press </publisher>
<Price> 17.00 </price>
</Book>
<Book>
<Title> windows networks </title>
<Author> paul </author>
<Publisher> white water press </publisher>
<Price> 32.99 </price>
</Book>
<Book>
<Title> fly leaf </title>
<Author> jenny </author>
<Publisher> white water press </publisher>
<Price> 19.50 </price>
</Book>
</Books>
|
*/
?>