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...