代码如下 | 复制代码 |
*/ public function deletecomment($id) { $xml = new domdocument(); $xml->validateonparse=true; $xml->loadxml($this->getcontents(true)); $message = $xml->getelementbyid($id); $parentnode = $message->parentnode; $parentnode->removechild($message); $this->putcontents($xml->savexml()); } /* xml 文档 <?xml version="1.0" encoding="utf-8"?> <!doctype messages [ <!element messages (message)*> <!element message (name , website? , comment , date , user_ip? , user_agent? , spam)> <!attlist message mid id #required> <!element name (#pcdata)> <!element website (#pcdata)> <!element comment (#pcdata)> <!element date (#pcdata)> <!element spam (#pcdata)> <!element user_ip (#pcdata)> <!element user_agent (#pcdata)> ]> <messages> </messages> |
*/
//-------------------------------------------------------------------------------------
代码如下 | 复制代码 |
function loadfile($file){ |
代码如下 | 复制代码 |
function: addcomment adds a comment. parameters: $data - associative array of data, must contain 'name', 'website', 'comment', 'date', 'user_ip', 'user_agent', and 'spam'. returns: id of the new comment. <?xml version="1.0" encoding="utf-8"?> <!doctype messages [ <!element messages (message)*> <!element message (name , website? , comment , date , user_ip? , user_agent? , spam)> ]> <messages> </messages> */ public function addcomment($data) { $xml = new simplexmlelement($this->getcontents(true)); $message = $xml->addchild('message'); $id = $this->generateid(); $message->addattribute('mid', $id); foreach ($data as $key => $value) { $message->addchild($key, htmlspecialchars($value, ent_quotes)); } $this->putcontents($xml->asxml()); return $id; } |
代码如下 | 复制代码 |
$xml = < < < xml out: [command_name] => provision ) ) |
<?
<?xml version="1.0′ encoding="utf-8′?>
<class>
<student>
<id>3</id>
<name>张三</name>
</student>
<student>
<id>5</id>
<name>李四</name>
</student>
</class>
$url="http://mb.111cn.net/ws.php?q=content";
$dom = new domdocument();
$dom->load($url);
//print"<pre>";
//print( $dom->savexml());
$dictarray = $dom->getelementsbytagname("dict");
foreach( $dictarray as $dict ) {
$key = $dict->getelementsbytagname("key");
$key=$key->item(0)->nodevalue;
$key= iconv("utf-8","gb2312",$key); //注意要转码对于中文,因为xml默认为utf-8格式
echo ("***输出>key>的内容:****:".$key);
$lang = $dict->getelementsbytagname("lang");
$lang = utf8_decode($lang->item(0)->nodevalue);
echo "<br>";
echo ("***输出>lang>的内容:****:".$lang);
}
方法三
$fp=fsockopen("xxx.xxx.com", 80, $errno, $errstr, 30);
if($fp){
$out = "get /abc.xml?key=value http/1.1 ";
$out .= "host: xxx.xxx.com ";
$out.= 'user_agent: xml-rpc client ';
$out.='content-type: text/xml ';
$out .= "connection: close ";
fwrite($fp,$out);
while(!@feof($fp)){
$result.=@fgets($fp,1024);
}
fclose($fp);
}
echo $result;
代码如下 | 复制代码 |
调用方法 if(!function_exists('xml_parser_create')) { //get the xml parser of php教程 - php must have this module for the parser to work if(!$xml_values) return;//hmm... //initializations $current = &$xml_array; //refference //go through the tags. //this command will extract these variables into the foreach scope $result = array(); if(isset($value)) { //set the attributes too. //see tag status and do the needed. $current = &$current[$tag]; } else { //there was another element with the same tag name if(isset($current[$tag][0])) {//if there is a 0th element it is already an array if(isset($current[$tag.'_attr'])) { //the attribute of the last(0th) tag must be moved as well } } elseif($type == "complete") { //tags that ends in 1 line '<tag />' } else { //if taken, put all things inside a list(array) // ...push the new element into that array. if($priority == 'tag' and $get_attributes and $attributes_data) { } else { //if it is not an array... $current[$tag]['0_attr'] = $current[$tag.'_attr']; if($attributes_data) { } elseif($type == 'close') { //end of tag '</tag>' return($xml_array); ######################################################################################################### class parserxml } if(!$xmlreaderror){ $children [$vals [$i]['tag']][] = $this->getchildren ($vals, $i, $type); ?> |