test.xml文件为
<xml>
<product value='abc'>
...
</product>
</xml>如何用 domxml取出'abc'?
domxml 太难用了。
100分感谢,太多的贴子没有答案了。都不知道结给谁。
<xml>
<product value='abc'>
...
</product>
</xml>如何用 domxml取出'abc'?
domxml 太难用了。
100分感谢,太多的贴子没有答案了。都不知道结给谁。
<?php
$dom = new DomDocument ();
$dom->load ( "test.xml" );
$elem = $dom->documentElement;
function Isc($str) {
if (preg_match ( "/[\x7f-\xff]/", $str )) {
return true;
} else {
return false;
}
}
function line($Str) {
$fp = fopen ( "test.xml", "r" ) or exit ( "未能打开该xml文件!" );
$line = 0;
while ( ! feof ( $fp ) ) {
$fg = fgets ( $fp );
if(strstr($fg,"<!")!=false){
$fg=fgets($fp);
}
$line ++;
if (strstr ( $fg, $Str ) != false)
echo "line: " . $line . "<br />";
}
fclose ( $fp );
}
function get_children($elem) {
switch ($elem->nodeType) {
case 1 :
if (strlen ( trim ( $elem->nodeName ) )) {
if (Isc ( trim ( $elem->nodeName ) )) {
line ( $elem->nodeName );
echo "类型:元素<br />";
echo trim ( $elem->nodeName ) . "<br><br><br>";
}
}
case 2 :
foreach ( $elem->attributes as $attr ) {
if (Isc ( $attr->nodeValue )) {
line ( $attr->nodeValue );
echo "类型: 属性<br>";
echo trim ( $attr->nodeValue ) . "<br><br><br>";
}
}
break;
case 3 :
if (strlen ( trim ( $elem->nodeValue ) )) {
if (Isc ( trim ( $elem->nodeValue ) )) {
line ( $elem->nodeValue );
echo "类型:文本<br />";
echo trim ( $elem->nodeValue ) . "<br><br><br>";
}
}
break;
}
$children = $elem->childNodes;
if (! empty ( $children ))
foreach ( $children as $elem ) {
get_children ( $elem );
}
}
get_children ( $elem );
?>
$dom->loadXML($xml);$ret = array();$xpath = new DOMXPath($dom);$query = '//xml/product[@value="abc"]';
$product = $xpath->query($query);
你要去接受“劳动”改造
The DOMXPath class
简介
Supports XPath 1.0 类摘要
DOMXPath {
/* 属性 */
public DOMDocument $document ;
/* 方法 */
DOMXPath::__construct ( DOMDocument $doc )
mixed DOMXPath::evaluate ( string $expression [, DOMNode $contextnode [, bool $registerNodeNS = true ]] )
DOMNodeList DOMXPath::query ( string $expression [, DOMNode $contextnode [, bool $registerNodeNS = true ]] )
bool DOMXPath::registerNamespace ( string $prefix , string $namespaceURI )
public void DOMXPath::registerPhpFunctions ([ mixed $restrict ] )
}
属性
document
Prop descriptionTable of Contents
DOMXPath::__construct — Creates a new DOMXPath object
DOMXPath::evaluate — Evaluates the given XPath expression and returns a typed result if possible.
DOMXPath::query — Evaluates the given XPath expression
DOMXPath::registerNamespace — Registers the namespace with the DOMXPath object
DOMXPath::registerPhpFunctions — Register PHP functions as XPath functions
呵,前辈已经迈向高级层次了。
tidy我也是最近才知道的,老牌的东西,很好用,可能你早就知道了我的习惯是:安全读入文件->检查是否可视文本->xml格式化->dom处理
因为我处理外来文件比较多,基本要全部执行上述几个步骤,你如果确定xml来源信任,可以适当跳过
to life169:
1.分数是浮云,解题是乐趣,尤其是遇到别人解题思路比自己高明的情况
2.我也是很懒,一般只说个思路,代码懒写
$dom->load("temp.xml");
$items=$dom->getElementsByTagName('Product'); echo $items-> item(0)->getattribute("value");我现在想不知道能否把第3行省掉。
先用着,等以后有时间再好好研究研究。
没办法,没时间啊。
请移步这里
http://topic.csdn.net/u/20110321/14/211774f1-bf78-42fe-a756-9458b9ecd2c8.html
hnxxwyq
请移步这里
http://topic.csdn.net/u/20110220/11/c77ec94c-e844-4a18-b52b-24fe7d239aa5.html
snmr_com
请移步这里
http://topic.csdn.net/u/20110110/14/aa313157-6042-4e18-a41c-5393fb17b97c.html
感谢life169
大伙帮忙让我把贴结了,给那些混分的人,实在不甘。