Php DOMElement operation xml document example _ PHP Tutorial

Source: Internet
Author: User
Example of DOMElement operation xml document in php. Copy the code as follows :? PhpStoreyourhtmlinto $ htmlvariable. $ htmlhtmlheadtitleRakeshVermatitleheadbodyahrefexample. comExampleaahrefgoogl The code is as follows:


// Store your html into $ html variable.
$ Html ="

Rakesh Verma


Example
Google
Yahoo

";
$ Dom = new DOMDocument ();
$ Dom-> loadHTML ($ html );
// Evaluate Anchor tag in HTML
$ Xpath = new DOMXPath ($ dom );
$ Hrefs = $ xpath-> evaluate ("/html/body // ");
For ($ I = 0; $ I <$ hrefs-> length; $ I ++ ){
$ Href = $ hrefs-> item ($ I );
$ Url = $ href-> getAttribute ('href ');
// Remove and set target attribute
$ Href-> removeAttribute ('target ');
$ Href-> setAttribute ("target", "_ blank ");
$ NewURL = $ url. ". au ";
// Remove and set href attribute
$ Href-> removeAttribute ('href ');
$ Href-> setAttribute ("href", $ newURL );
}
// Save html
$ Html = $ dom-> saveHTML ();
Echo $ html;
?>


Example 2

The code is as follows:


/*




<班级>
<学生 number="101">
<名字> Sun Wukong
<名字> Sun Walker
<年龄> 123
<介绍>&*$%特殊字串^&#$&

<学生 number="10" 2">
<名字> Bai Gujing
<年龄> 140
<介绍> Introduction


*/
$ Xmldoc = new DOMDocument ('1. 0', 'utf-8 ');
$ Xmldoc-> load ('datas. XML ');
$ ItemsNodeList = $ xmldoc-> getElementsbyTagName ('studen ');
$ ItemElement = $ itemsNodeList-> item (0); // obtain the first complete student information node.
$ ItemChildsNodeList = $ itemElement-> getElementsbyTagName ('name'); // Obtain the subnode "name", which may have multiple names
$ ItemChildNode = $ itemChildsNodeList-> item (0); // obtain the first name node.
Echo $ itemChildNode-> nodeValue; // output node value
// Encapsulate it into a function
$ NodeArr = array ('name', 'age', 'Introduction ');
Function getNodeVal ($ xmldoc, $ itemsName, $ nodeArr ){
$ Items = $ xmldoc-> getElementsByTagName ($ itemsName );
For ($ I = 0; $ I <$ items-> length; $ I ++ ){
$ Item = $ items-> item ($ I );
Foreach ($ nodeArr as $ node ){
$ Data [$ I] [] = $ item-> getElementsByTagName ($ node)-> item (0)-> nodeValue;
}
}
Return $ data;
}
$ Data = getNodeVal ($ xmldoc, 'Student ', $ nodeArr );
Print_r ($ data );

The http://www.bkjia.com/PHPjc/326712.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/326712.htmlTechArticle code is as follows :? Php // Store your html into $ html variable. $ html = "html head titleRakesh Verma/title/head body a href = 'http: // example.com 'Example/a href = 'http: // googl...

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.