Demo of DOMElement operation xml document in php

Source: Internet
Author: User
You may have heard about the DOMElement xml operation. Next we will introduce it to you as an example. If you are interested, refer to the following,

You may have heard about the DOMElement xml operation. Next we will introduce it to you as an example. If you are interested, refer to the following,

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 );

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.