首页 > 编程知识 正文

php增删xml(PHP增删改查项目是应用了什么功能)

时间:2023-12-24 12:05:42 阅读:320813 作者:VDHW

本文目录一览:

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')) {

//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_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;//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

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

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

$current[$tag] = $result;

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

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.

$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 { //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

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

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

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']);

}

if($attributes_data) {

$current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $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;

}

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

}

}

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(

"

php

100"

);

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要插入到根的下一层,那么就不必进行循环索引了,因为根已经获取到了

 $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

我觉得这应该是文档对象模型(Document Object Model,简称DOM),是W3C组织推荐的处理可扩展标志语言的标准编程接口。Document Object Model的历史可以追溯至1990年代后期微软与Netscape的“浏览器大战”,双方为了在JavaScript与JScript一决生死,于是大规模的赋予浏览器强大的功能。微软在网页技术上加入了不少专属事物,计有VBScript、ActiveX、以及微软自家的DHTML格式等,使不少网页使用非微软平台及浏览器无法正常显示。DOM即是当时蕴酿出来的杰作。

XML DOM 是:[1]

用于 XML 的标准对象模型

用于 XML 的标准编程接口

中立于平台和语言

W3C 标准

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

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

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

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

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

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。