Login
网站首页 > 文章中心 > 其它

php增删xml_PHP增删改查

作者:小编 更新时间:2023-09-25 16:12:46 浏览量:103人看过

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转换成数组

调用方法

*/

if(!$contents) return array();

if(!function_exists('xml_parser_create')) {

//print "'xml_parser_create()' function not found!";

return array();

}

//get the xml parser of php教程 - php must have this module for the parser to work

$parser = xml_parser_create('');

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;//hmm...

//initializations

$xml_array = array();

$parents = array();

$opened_tags = array();

$arr = array();

$current = $xml_array; //refference

//go through the tags.

$repeated_tag_index = array();//multiple tags with same name will be turned into an array

foreach($xml_values as $data) {

unset($attributes,$value);//remove existing values, or there will be trouble

//this command will extract these variables into the foreach scope

// tag(string), type(string), level(int), attributes(array).

extract($data);//we could use the array by itself, but this cooler.

$result = array();

$attributes_data = array();

if(isset($value)) {

if($priority == 'tag') $result = $value;

else $result['value'] = $value; //put the value in a assoc array if we are in the 'attribute' mode

//set the attributes too.

if(isset($attributes) and $get_attributes) {

foreach($attributes as $attr = $val) {

if($priority == 'tag') $attributes_data[$attr] = $val;

else $result['attr'][$attr] = $val; //set all the attributes in a array called 'attr'

//see tag status and do the needed.

if($type == "open") {//the starting of the tag 'tag'

$parent[$level-1] = $current;

if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //insert new tag

$current[$tag] = $result;

if($attributes_data) $current[$tag. '_attr'] = $attributes_data;

$repeated_tag_index[$tag.'_'.$level] = 1;

$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

$current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;

$repeated_tag_index[$tag.'_'.$level]◆◆;

} else {//this section will make the value an array if multiple tags with the same name appear together

$current[$tag] = array($current[$tag],$result);//this will combine the existing item and the new item together to make an array

if(isset($current[$tag.'_attr'])) { //the attribute of the last(0th) tag must be moved as well

$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") { //tags that ends in 1 line 'tag /'

//see if the key is already taken.

if(!isset($current[$tag])) { //new key

if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data;

} else { //if taken, put all things inside a list(array)

if(isset($current[$tag][0]) and is_array($current[$tag])) {//if it is already an array...

// ...push the new element into that array.

if($priority == 'tag' and $get_attributes and $attributes_data) {

$current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;

} else { //if it is not an array...

$current[$tag] = array($current[$tag],$result); //...make it an array using using the existing value and the new value

if($priority == 'tag' and $get_attributes) {

if($attributes_data) {

$repeated_tag_index[$tag.'_'.$level]◆◆; //0 and 1 index is already taken

} elseif($type == 'close') { //end of tag '/tag'

$current = $parent[$level-1];

return($xml_array);

#########################################################################################################

class parserxml

{

function parserxml()

function parserxmlbyurl($url)

$fp = @fopen($url,'r');

if (!$fp){

return 1;

@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;

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 {

$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 ();

$attributes [$attkey] = $vals [$i]['attributes'][$attkey];

if ($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);

$value = $children [$vals [$i]['tag']];

$children [$vals [$i]['tag']]['value'] = $value;

$children [$vals [$i]['tag']] = array_merge ($children [$vals [$i]['tag']], $attributes);

return $children;

在线等 PHP如何操作XML

试编写代码如下:

php

$xml = new DOMDocument();

$xml-load('test.xml');

$data = $xml-documentElement;

$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执行

操作

$doc

=

new

DOMDocument();

$doc-load(

'index.

'

);

$root

-

documentElement;

$books

$doc-getElementsByTagName(

"

①.00"

foreach(

as

$book

){

echo

$book-getAttribute('id')."-";

$book-getElementsByTagName(

"index"

)-item(0)-nodeValue='test

test';

"br";

if($book-getAttribute('id')==1){

$root-removeChild($book);

$doc-save('index.

');

PHP操作xml的问题,如何插入数据

土嘎嘎的粉丝们大家好,PHP操作XML新人为你解答!

$xml?=?simplexml_load_file($xmlsrc);//初始化并获取其根

//看得出来,你的user要插入到根的下一层,那么就不必进行循环索引了,因为根已经获取到了

$user?=?$xml-addChild("user",?"");//在$xml节点下插入名为user的节点,且必须赋值,这里因为没有值,所以就留空,但不能不写.

//然后再在user节点下插入节点,这里已经把user节点赋给$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

XML DOM 是:[1]

用于 XML 的标准对象模型

用于 XML 的标准编程接口

中立于平台和语言

XML DOM 定义了所有 XML 元素的对象和属性,以及访问它们的方法(接口).

换句话说:XML DOM 是用于获取、更改、添加或删除 XML 元素的标准.

SimpleXML 函数允许您把 XML 转换为对象.

通过普通的属性选择器或数组迭代器,可以处理这个对象,就像处理任何其他对象一样.

其中的一些函数需要最新的 PHP 版本..如果还有不懂的可以上后盾人看看相关的教材,多看几遍不懂的不就少了.希望对你有帮助.

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章