php 에서 DOMElement 동작 xml 문서 실례 시범

3167 단어 DOMElementxml
 
<?php
//Store your html into $html variable.
$html="<html>
<head>
<title>Rakesh Verma</title>
</head>
<body>
<a href='http://example.com'>Example</a>
<a href='http://google.com'>Google</a>
<a href='http://www.yahoo.com'>Yahoo</a>
</body>
</html>";
$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;
?>
예 2
 
/*<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- css , 。 :name{color:red;} -->
<?xml-stylesheet type="text/css" href="css.css"?>
<!-- dtd ( : )<!DOCTYPE SYSTEM "class.dtd" /> -->
<!-- <!DOCTYPE [
<!ELEMENT ( +)>
<!ELEMENT ( , , )>
<!ELEMENT (#PCDATA)>
<!ELEMENT (#PCDATA)>
<!ELEMENT (#PCDATA)>
] /> -->
< >
< number="101">
< > </ >
< > </ >
< >123</ >
< ><![CDATA[&*$% ^&#$&]]></ >
</ >
< number="10&quot;2">
< > </ >
< >140</ >
< > </ >
</ >
</ >
*/
$xmldoc = new DOMDocument('1.0', 'UTF-8');
$xmldoc->load('datas.xml');
$itemsNodeList = $xmldoc->getElementsbyTagName(' ');
$itemElement = $itemsNodeList->item(0);//
$itemChildsNodeList = $itemElement->getElementsbyTagName(' ');// “ ”,
$itemChildNode = $itemChildsNodeList->item(0);//
echo $itemChildNode->nodeValue;//
//
$nodeArr = array(' ', ' ', ' ');
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, ' ', $nodeArr);
print_r($data);

좋은 웹페이지 즐겨찾기