test.xml文件为
<xml>
<product value='abc'>
...
</product>
</xml>如何用 domxml取出'abc'?
domxml 太难用了。
100分感谢,太多的贴子没有答案了。都不知道结给谁。

解决方案 »

  1.   

    这是我写的用递归的方法去取xml文件里的节点名、文本内容、还有属性值 并且判断是否含有中文的代码,楼主可以参考下
       <?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 );
    ?>
      

  2.   

    $dom = new DOMDocument('1.0', 'GBK');
    $dom->loadXML($xml);$ret = array();$xpath = new DOMXPath($dom);$query = '//xml/product[@value="abc"]';
    $product = $xpath->query($query);
      

  3.   


    你要去接受“劳动”改造
    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
      

  4.   

    谢,我这里loadxml 总是报错,说文件没有以'<'开始,改用load,不知二者是否一样?
      

  5.   

    谢,google 把我这个菜鸟带到浏览器的xpath了。
    呵,前辈已经迈向高级层次了。
      

  6.   

    load之前要处理一下文件,我建议你养成习惯,总是信任文件不太好或者用二进制安全的方式读入为字符串,用tidy或可想像方式把该字符串格式化为xml在应用处理
    tidy我也是最近才知道的,老牌的东西,很好用,可能你早就知道了我的习惯是:安全读入文件->检查是否可视文本->xml格式化->dom处理
    因为我处理外来文件比较多,基本要全部执行上述几个步骤,你如果确定xml来源信任,可以适当跳过
    to life169:
    1.分数是浮云,解题是乐趣,尤其是遇到别人解题思路比自己高明的情况
    2.我也是很懒,一般只说个思路,代码懒写
      

  7.   

    谢谢各位。在cu得到一个适合我简单的答案 $dom = new DOMDocument();
    $dom->load("temp.xml");
    $items=$dom->getElementsByTagName('Product'); echo $items-> item(0)->getattribute("value");我现在想不知道能否把第3行省掉。
    先用着,等以后有时间再好好研究研究。
    没办法,没时间啊。
      

  8.   

    MMDD0827
    请移步这里
    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
    大伙帮忙让我把贴结了,给那些混分的人,实在不甘。
      

  9.   

        说得很好,分数是浮云,争取拿个PHP版的花花,以后就再也不要花了。没什么意思,感觉花多!==技术厉害,只是一种虚荣心在作怪而已!楼上的第一,第二这个月不要和我争虚荣心哦。哈哈!开个玩笑半夜路过!