本文目录一览:
- PHP添加XML内容
- [PHP XML删除的问题](#PHP XML删除的问题)
- 在线等 PHP如何操作XML
- 求PHP对XML增加删除修改的方法或者类
- PHP操作xml的问题,如何插入数据
- 请教,PHP使用xml
PHP添加XML内容
最后第二句添加有错 应该这样写:
$a = $doc->createElement('a'); //定义一个a标签
$a_text = $doc->createTextNode("0000000000"); //给a标签一个值
$a->appendChild($a_text); //把值加载到a标签
$id = $doc->createAttribute('id'); //定义一个id属性
$id_text = $doc->createTextNode("xxx"); //给id属性一个值
$id->appendChild($id_text); //把值加载到id属性中
$a->appendChild($id); //把id属性加载到a标签中
$php100->appendChild($a); //把a标签加载到php100标签中
PHP XML删除的问题
将xml转成数组,然后再删除数据中的值,更方便 下面是转换成数组了函数 xml转换成数组 调用方法
$array = xml2array(file_get_contents('feed.xml', 1, 'attribute'));
function xml2array($contents, $get_attributes=1, $priority = 'tag') {
if(!$contents) return array();
if(!function_exists('xml_parser_create')) {
return array();
}
$parser = xml_parser_create('');
xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "utf-8");
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, trim($contents), $xml_values);
xml_parser_free($parser);
if(!$xml_values) return;
$xml_array = array();
$parents = array();
$opened_tags = array();
$arr = array();
$current = &$xml_array;
$repeated_tag_index = array();
foreach($xml_values as $data) {
unset($attributes,$value);
extract($data);
$result = array();
$attributes_data = array();
if(isset($value)) {
if($priority == 'tag') $result = $value;
else $result['value'] = $value;
}
if(isset($attributes) and $get_attributes) {
foreach($attributes as $attr=>$val) {
if($priority == 'tag') $attributes_data[$attr] = $val;
else $result['attr'][$attr] = $val;
}
}
if($type == "open") {
$parent[$level-1] = &$current;
if(!is_array($current) or (!in_array($tag, array_keys($current)))) {
$current[$tag] = $result;
if($attributes_data) $current[$tag. '_attr'] = $attributes_data;
$repeated_tag_index[$tag.'_'.$level] = 1;
$current = &$current[$tag];
} else {
if(isset($current[$tag][0])) {
$current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
$repeated_tag_index[$tag.'_'.$level]++;
} else {
$current[$tag] = array($current[$tag],$result);
$repeated_tag_index[$tag.'_'.$level] = 2;
if(isset($current[$tag.'_attr'])) {
$current[$tag]['0_attr'] = $current[$tag.'_attr'];
unset($current[$tag.'_attr']);
}
}
$last_item_index = $repeated_tag_index[$tag.'_'.$level]-1;
$current = &$current[$tag][$last_item_index];
}
} elseif($type == "complete") {
if(!isset($current[$tag])) {
$current[$tag] = $result;
$repeated_tag_index[$tag.'_'.$level] = 1;
if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data;
} else {
if(isset($current[$tag][0]) and is_array($current[$tag])) {
$current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
if($priority == 'tag' and $get_attributes and $attributes_data) {
$current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
}
$repeated_tag_index[$tag.'_'.$level]++;
} else {
$current[$tag] = array($current[$tag],$result);
$repeated_tag_index[$tag.'_'.$level] = 1;
if($priority == 'tag' and $get_attributes) {
if(isset($current[$tag.'_attr'])) {
$current[$tag]['0_attr'] = $current[$tag.'_attr'];
unset($current[$tag.'_attr']);
}
if($attributes_data) {
$current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
}
}
$repeated_tag_index[$tag.'_'.$level]++;
}
}
} elseif($type == 'close') {
$current = &$parent[$level-1];
}
}
return($xml_array);
}
class parserxml
{
function parserxml()
{
}
function parserxmlbyurl($url)
{
$fp = @fopen($url,'r');
if (!$fp){
return 1;
}
$xmlstr = fread($fp,1024);
@fclose($fp);
if ($xmlstr === false){
return 0;
}
return $this->getxmltree($xmlstr);
}
function getxmltree($xmldata)
{
ini_set ('track_errors', '1');
$xmlreaderror = false;
$parser = xml_parser_create();
xml_parser_set_option ($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parser_set_option ($parser, XML_OPTION_CASE_FOLDING, 0);
if (!xml_parse_into_struct ($parser, $xmldata, $vals, $index)) {
$xmlreaderror = true;
return 0;
}
xml_parser_free($parser);
if(!$xmlreaderror){
$result = array ();
$i = 0;
if (isset($vals[$i]['attributes'])){
foreach (array_keys ($vals [$i]['attributes']) as $attkey)
$attributes[$attkey] = $vals[$i]['attributes'][$attkey];
}
$result[$vals [$i]['tag']] = array_merge((array)$attributes, $this->getchildren($vals, $i, 'open'));
}
ini_set('track_errors', '0');
return $result;
}
function getchildren ($vals, $i, $type)
{
if ($type == 'complete') {
if (isset ($vals [$i]['value']))
return ($vals [$i]['value']);
else
return '';
}
$children = array ();
while ($vals [++$i]['type'] != 'close') {
$type = $vals [$i]['type'];
if (isset ($children [$vals [$i]['tag']])) {
if (is_array ($children [$vals [$i]['tag']])) {
$temp = array_keys ($children [$vals [$i]['tag']]);
if (is_string ($temp [0])) {
$a = $children [$vals [$i]['tag']];
unset ($children [$vals [$i]['tag']]);
$children [$vals [$i]['tag']][0] = $a;
}
} else {
$a = $children [$vals [$i]['tag']];
unset ($children [$vals [$i]['tag']]);
$children [$vals [$i]['tag']][0] = $a;
}
$children [$vals [$i]['tag']][] = $this->getchildren ($vals, $i, $type);
} else
$children [$vals [$i]['tag']] = $this->getchildren ($vals, $i, $type);
if (isset ($vals [$i]['attributes'])) {
$attributes = array ();
foreach (array_keys ($vals [$i]['attributes']) as $attkey)
$attributes [$attkey] = $vals [$i]['attributes'][$attkey];
if (isset ($children [$vals [$i]['tag']])) {
if ($children [$vals [$i]['tag']] == '') {
unset ($children [$vals [$i]['tag']]);
$children [$vals [$i]['tag']] = $attributes;
} elseif (is_array ($children [$vals [$i]['tag']])) {
$index = count ($children [$vals [$i]['tag']]) - 1;
if ($children [$vals [$i]['tag']][$index] == '') {
unset ($children [$vals [$i]['tag']][$index]);
$children [$vals [$i]['tag']][$index] = $attributes;
}
$children [$vals [$i]['tag']][$index] = array_merge ($children [$vals [$i]['tag']][$index], $attributes);
} else {
$value = $children [$vals [$i]['tag']];
unset ($children [$vals [$i]['tag']]);
$children [$vals [$i]['tag']]['value'] = $value;
$children [$vals [$i]['tag']] = array_merge ($children [$vals [$i]['tag']], $attributes);
}
} else {
$children [$vals [$i]['tag']] = $attributes;
}
} else {
$children [$vals [$i]['tag']] = $attributes;
}
}
return $children;
}
}
在线等 PHP如何操作XML
试编写代码如下:
<?php
$xml = new DOMDocument();
$xml->load('test.xml');
$data = $xml->documentElement;
$pic = $data->getElementsByTagName('pic')->item(8);
$newpic = $data->removeChild($pic);
$first = $data->getElementsByTagName('pic')->item(0);
$newpic->setAttribute('src','0000.pic');
$newpic->setAttribute('tag','jpeg');
$newpic->setAttribute('link','');
$newpic->setAttribute('title','0000');
$newpic->setAttribute('content','0000');
$data->insertBefore($newpic,$first);
$xml->save('test.xml');
?>
求PHP对XML增加删除修改的方法或者类
修改和删除XML内容的原理和方法:
- 修改:通过查询内容的原理重新对原始内容进行赋值操作,需要通过ID确定位置
- 删除:首先确定到跟标签,使用
removeChild
方法找到指定id执行删除操作
<?php
$doc = new DOMDocument();
$doc->load('index.XML');
$root = $doc->documentElement;
$books = $doc->getElementsByTagName("php100");
foreach($books as $book){
if($book->getAttribute('id')==2){
echo $book->getAttribute('id')."-";
echo $book->getElementsByTagName("index")->item(0)->nodeValue='test test';
echo "<br>";
}
if($book->getAttribute('id')==1){
$root->removeChild($book);
}
}
$doc->save('index.XML');
?>
PHP操作xml的问题,如何插入数据
你好,PHP操作XML新人为你解答!
$xml = simplexml_load_file($xmlsrc); //初始化并获取其根
$user = $xml->addChild("user", ""); //在$xml节点下插入名为user的节点,且必须赋值,这里因为没有值,所以就留空,但不能不写。
$name = $user->addChild("name", $_POST["name"]); //把接收到的name值添加到user节点下的name节点中,下同
$sex = $user->addChild("sex", $_POST["sex"]); //因为这里的name和sex添加后不再用了,可以不把节点赋值出来,即不须写上$sex=
$hei = $user->addChild("height", $_POST["height"]);
$hei->addAttribute("type", "cm"); //给height节点添加属性
这个SimpleXML我昨天才开始学,感觉挺不错的样子,操作代码直观,可读性强,多用用熟练了就好。
请教,PHP使用xml
我觉得这应该是文档对象模型(Document Object Model,简称DOM),是W3C组织推荐的处理可扩展标志语言的标准编程接口。Document Object Model的历史可以追溯至1990年代后期微软与Netscape的“浏览器大战”,双方为了在JavaScript与JScript一决生死,于是大规模的赋予浏览器强大的功能。微软在网页技术上加入了不少专属事物,计有VBScript、ActiveX、以及微软自家的DHTML格式等,使不少网页使用非微软平台及浏览器无法正常显示。DOM即是当时蕴酿出来的杰作。 XML DOM 是:
- 用于 XML 的标准对象模型
- 用于 XML 的标准编程接口
- 中立于平台和语言
- W3C 标准 XML DOM 定义了所有 XML 元素的对象和属性,以及访问它们的方法(接口)。 换句话说:XML DOM 是用于获取、更改、添加或删除 XML 元素的标准。 SimpleXML 函数允许您把 XML 转换为对象。 通过普通的属性选择器或数组迭代器,可以处理这个对象,就像处理任何其他对象一样。 其中的一些函数需要最新的 PHP 版本。如果还有不懂的可以上后盾人看看相关的教材,多看几遍不懂的不就少了。希望对你有帮助。