100分求php处理XML格式字符串的例子 本帖最后由 yhlovezx 于 2009-11-13 14:24:43 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 通常是用php读出XML字符串转换为数组,然后对数组进行操作 $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++;} 谢谢楼上的,simplexml解析和修改节点属性没问题,但是不知道有没有办法增加和删除节点内容 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()); 这里有谁用phpmyadmin的吗 phpMyAdmin -config.inc.php文件中的root帐号的密码问题? php中如下两函数,jsp中可有对应功能的 php正则匹配 请专家帮忙 请问这一段网上的socket操作为何报http status error 错误? 一台机器可以装两个PHP么,版本不同 实现C/S方式下客户端零维护的脚本如何写? 如何读写文件? 请教 对现在服务器技术而言,shtml 和html还有区别吗 超级奇怪的IF判断语句 phpeclipse:the selection cannot be launched
$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++;
}
simplexml解析和修改节点属性没问题,
但是不知道有没有办法增加和删除节点内容
$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());