本帖最后由 yhlovezx 于 2009-11-13 14:24:43 编辑

解决方案 »

  1.   

    通常是用php读出XML字符串转换为数组,然后对数组进行操作
      

  2.   


    $test=" <root act='xx'> <item id='1' lesson='111' /> <item id='2' lesson='222' /> </root>"; 
    $xml = simplexml_load_string($test);
    $i = 1;
    foreach ($xml->children() as $child){
    if($i==1){
    foreach ($child->attributes() as $k => $v){
    echo $k.":".$v;
    }
    }
    $i++;
    }
      

  3.   

    谢谢楼上的,
    simplexml解析和修改节点属性没问题,
    但是不知道有没有办法增加和删除节点内容
      

  4.   

    dom那一块挺多东西的,多找找手册.
    $test="<root act='xx'><item id='1' lesson='111'/><item id='2' lesson='222'/></root>"; 
    $dom = new DOMDocument;
    $dom->loadXML($test);
    $xpath = new DOMXPath($dom);
    $item = $xpath->query("//root/item[@id='1']");
    //遍历属性
    foreach($item->item(0)->attributes as $name=>$value)
    {
    echo $name.":".$value->value."<br/>";
    }
    //可以根据属性名读值
    //echo $item->item(0)->getAttribute('lesson');//添加属性
    $item->item(0)->setAttribute("hello","world");
    //修改属性
    $item->item(0)->setAttribute("lesson","math");
    //删除属性
    $item->item(0)->removeAttribute("id");
    //打印结果
    echo htmlspecialchars($dom->saveXML());