Use PHP to read and write xml dom code

Source: Internet
Author: User

Copy codeThe Code is as follows:
// Read XML with DOM
$ Doc = new DOMDocument ();
$ Doc-> load ('test. xml ');
$ Books = $ doc-> getElementsByTagName ("book ");
Foreach ($ books as $ book ){
$ Authors = $ book-> getElementsByTagName ("author ");
$ Author = $ authors-> item (0)-> nodeValue; // nodeValue attributes can be set or returned Based on the node type.
$ Publishers = $ book-> getElementsByTagName ("publisher ");
$ Publisher = $ publishers-> item (0)-> nodeValue;
$ Titles = $ book-> getElementsByTagName ("title ");
$ Title = $ titles-> item (0)-> nodeValue;
Echo "Title: $ title <br> Author: $ author <br> Publisher: $ publisher <br> }

/*
The script first creates a new DOMdocument object and loads the library XML into this object using the load method. Then, the script uses the getElementsByName method to obtain a list of all elements under the specified name.
In the cycle of the book node, the script uses the getElementsByName method to obtain the nodeValue marked by author, publisher, and title. NodeValue is the text in the node. The script then displays these values.
*/
Copy codeThe Code is as follows:
// Use the SAX Parser to read XML
$ G_books = array ();
$ G_elem = null;
Function startElement ($ parser, $ name, $ attrs ){
Global $ g_books, $ g_elem;
If ($ name = 'book') $ g_books [] = array ();
$ G_elem = $ name;
}
Function endElement ($ parser, $ name ){
Global $ g_elem;
$ G_elem = null;
}
Function textData ($ parser, $ text ){
Global $ g_books, $ g_elem;
If ($ g_elem = 'author' | $ g_elem = 'her her '| $ g_elem = 'title '){
$ G_books [count ($ g_books)-1] [$ g_elem] = $ text;
}
}
$ Parser = xml_parser_create ();
Xml_set_element_handler ($ parser, "startElement", "endElement ");
Xml_set_character_data_handler ($ parser, "textData ");
$ F = fopen ('test. xml', 'R ');
While ($ data = fread ($ f, 4096 )){
Xml_parse ($ parser, $ data );
}
Xml_parser_free ($ parser );
Foreach ($ g_books as $ book ){
Echo $ book ['title']. "-". $ book ['author']. "-";
Echo $ book ['her her ']. "\ n ";
}

/*
The script first sets the g_books array, which contains all books and book information in the memory. The g_elem variable saves the name of the tag currently being processed by the script. Define the callback function in the script. In this example, the callback functions are startElement, endElement, and textData. Call the startElement and endElement functions when opening and disabling tags respectively. Call textData on the text between the start and end tags.
In this example, the startElement tag finds the book tag and starts a new element in the book array. Then, the textData function checks the current element to see if it is a publisher, title, or author flag. If yes, the function puts the current text into the current graph book.
To continue the parsing, the script uses the xml_parser_create function to create a parser. Then, set the callback handle. Then, the script reads the file and sends the large part of the file to the parser. After the file is read, The xml_parser_free function deletes the parser. The content of the g_books array is output at the end of the script.
*/
// Parse XML using regular expressions
Copy codeThe Code is as follows:
$ Xml = "";
$ F = fopen ('test. xml', 'R ');
While ($ data = fread ($ f, 4096) {$ xml. = $ data ;}
Fclose ($ f );
Preg_match_all ("/\ <book \> (.*?) \ <\/Book \>/s ", $ xml, $ bookblocks );
Foreach ($ bookblocks [1] as $ block ){
Preg_match_all ("/\ <author \> (.*?) \ <\/Author \>/", $ block, $ author );
Preg_match_all ("/\ <title \> (.*?) \ <\/Title \>/", $ block, $ title );
Preg_match_all ("/\ <publisher \> (.*?) \ <\/Publisher \>/", $ block, $ publisher );
Echo ($ title [1] [0]. "-". $ author [1] [0]. "-". $ publisher [1] [0]. "\ n ");
}

/*
I never recommend using regular expressions to read XML, but sometimes it is the best compatibility, because regular expression functions are always available. Do not use regular expressions to read XML directly from the user, because the format or structure of such XML cannot be controlled. You should always use the DOM library or the SAX Parser to read the XML from the user.
*/
// Write XML with DOM
Copy codeThe Code is as follows:
$ Books = array ();
$ Books [] = array (
'Title' => 'php hacks ',
'Author' => 'Jack Herrington ',
'Her her '=> "O 'Reilly"
);
$ Books [] = array (
'Title' => 'podcasting hacks ',
'Author' => 'Jack Herrington ',
'Her her '=> "O 'Reilly"
);
$ Doc = new DOMDocument ();
$ Doc-> formatOutput = true;
$ R = $ doc-> createElement ("books ");
$ Doc-> appendChild ($ r );
Foreach ($ books as $ book ){
$ B = $ doc-> createElement ("book ");
$ Author = $ doc-> createElement ("author ");
$ Author-> appendChild ($ doc-> createTextNode ($ book ['author']);
$ B-> appendChild ($ author );
$ Title = $ doc-> createElement ("title ");
$ Title-> appendChild ($ doc-> createTextNode ($ book ['title']);
$ B-> appendChild ($ title );
$ Publisher = $ doc-> createElement ("publisher ");
$ Publisher-> appendChild ($ doc-> createTextNode ($ book ['her her ']);
$ B-> appendChild ($ publisher );
$ R-> appendChild ($ B );
}
// Echo $ doc-> saveXML ();

/*
At the top of the script, some example books are loaded into the books array. This data can be from the user or the database.
After the example book is loaded, the script creates a new DOMDocument and adds the root node books to it. Then, the script creates a node for the author, title, and publisher of each book, and adds a text node to each node. The last step of each book node is to add it to the root node books.
The real value of using DOM is that the XML it creates is always in the correct format. But what should I do if I cannot use DOM to create XML?
Xml Code
Copy codeThe Code is as follows:
<? Php
PHP compiling xml
$ Books = array ();
$ Books [] = array (
'Title' => 'php hacks ',
'Author' => 'Jack Herrington ',
'Her her '=> "O 'Reilly"
);
$ Books [] = array (
'Title' => 'podcasting hacks ',
'Author' => 'Jack Herrington ',
'Her her '=> "O 'Reilly"
);
?>
<Books>
<? Php
Foreach ($ books as $ book)
{
?>
<Book>
<Title> <? Php echo ($ book ['title']);?> </Title>
<Author> <? Php echo ($ book ['author']);?>
</Author>
<Publisher> <? Php echo ($ book ['her her ']);?>
</Publisher>
</Book>
<? Php
}
?>
</Books>

The test. xml used in the instance is as follows:
Copy codeThe Code is as follows:
<? Xml version = "1.0" encoding = "utf8"?>
<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>

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.