PHP domelement operation XML document instance demo _php skill

Source: Internet
Author: User
Copy Code code as follows:

<?php
Store your HTML into $html variable.
$html = "

<title>rakesh verma</title>

<body>
<a href= ' http://example.com ' >Example</a>
<a href= ' http://google.com ' >Google</a>
<a href= ' http://www.yahoo.com ' >Yahoo</a>
</body>
$dom = new DOMDocument ();
$dom->loadhtml ($html);
Evaluate Anchor tag in HTML
$xpath = new Domxpath ($dom);
$hrefs = $xpath->evaluate ("/html/body//a");
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
Copy Code code as follows:

/*<?xml version= "1.0" encoding= "UTF-8" standalone= "no"?>
<!--CSS style definition, no dot. such as: name{color:red;}-->
<?xml-stylesheet type= "Text/css" href= "Css.css"?>
<!--introduce a DTD document definition file (root element: Class) <! DOCTYPE class SYSTEM "Class.dtd"/>-->
<!--<! DOCTYPE class [
<! ELEMENT Class (student +) >
<! ELEMENT Student (name, age, Introduction) >
<! ELEMENT name (#PCDATA) >
<! ELEMENT Age (#PCDATA) >
<! ELEMENT Introduction (#PCDATA) >
]/>-->
< class >
< students ' "number=" >
< name > Monkey King </name >
< name > Sun Xing </name >
< age >123</Age >
< introduction ><! [cdata[&*$% Special string ^&#$&]]></introduction >
</Students >
< student number= "2" >
< name > Genie </name >
< age >140</Age >
< Introduction > Introduction content </Introduction >
</Students >
</class >
*/
$xmldoc = new DOMDocument (' 1.0 ', ' UTF-8 ');
$xmldoc->load (' datas.xml ');
$itemsNodeList = $xmldoc->getelementsbytagname (' student ');
$itemElement = $itemsNodeList->item (0);//Get the first complete student information node
$itemChildsNodeList = $itemElement->getelementsbytagname (' name ');//Get child node "name", maybe have more than one name
$itemChildNode = $itemChildsNodeList->item (0);//Get First name node
echo $itemChildNode->nodevalue;//Output node value
Encapsulated into functions
$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.