php增刪xml(PHP增刪改查項目是應用了什麼功能)

本文目錄一覽:

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 版本。。如果還有不懂的可以上後盾人看看相關的教材,多看幾遍不懂的不就少了。希望對你有幫助。

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/152531.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-11-13 06:05
下一篇 2024-11-13 06:05

相關推薦

  • PHP和Python哪個好找工作?

    PHP和Python都是非常流行的編程語言,它們被廣泛應用於不同領域的開發中。但是,在考慮擇業方向的時候,很多人都會有一個問題:PHP和Python哪個好找工作?這篇文章將從多個方…

    編程 2025-04-29
  • 掌握magic-api item.import,為你的項目注入靈魂

    你是否曾經想要導入一個模塊,但卻不知道如何實現?又或者,你是否在使用magic-api時遇到了無法導入的問題?那麼,你來到了正確的地方。在本文中,我們將詳細闡述magic-api的…

    編程 2025-04-29
  • PHP怎麼接幣

    想要在自己的網站或應用中接受比特幣等加密貨幣的支付,就需要對該加密貨幣擁有一定的了解,並使用對應的API進行開發。本文將從多個方面詳細闡述如何使用PHP接受加密貨幣的支付。 一、環…

    編程 2025-04-29
  • Java和Python哪個功能更好

    對於Java和Python這兩種編程語言,究竟哪一種更好?這個問題並沒有一個簡單的答案。下面我將從多個方面來對Java和Python進行比較,幫助讀者了解它們的優勢和劣勢,以便選擇…

    編程 2025-04-29
  • GitHub好玩的開源項目

    本文旨在介紹GitHub上一些好玩的開源項目,並提供代碼示例供讀者參考和學習。 一、Emoji列表 GitHub上有一份完整的Emoji列表,它支持各種平台和設備,方便用戶在Git…

    編程 2025-04-28
  • 如何將Java項目分成Modules並使用Git進行版本控制

    本文將向您展示如何將Java項目分成模塊,並使用Git對它們進行版本控制。分割Java項目可以使其更容易維護和拓展。Git版本控制還可以讓您跟蹤項目的發展並協作開發。 一、為什麼要…

    編程 2025-04-28
  • Django框架:從簡介到項目實戰

    本文將從Django的介紹,以及如何搭建Django環境開始,逐步深入到Django模型、視圖、模板、表單,最後通過一個小型項目實戰,進行綜合性的應用,讓讀者獲得更深入的學習。 一…

    編程 2025-04-28
  • Python每次運行變量加一:實現計數器功能

    Python編程語言中,每次執行程序都需要定義變量,而在實際開發中常常需要對變量進行計數或者累加操作,這時就需要了解如何在Python中實現計數器功能。本文將從以下幾個方面詳細講解…

    編程 2025-04-28
  • 使用PHP foreach遍歷有相同屬性的值

    本篇文章將介紹如何使用PHP foreach遍歷具有相同屬性的值,並給出相應的代碼示例。 一、基礎概念 在講解如何使用PHP foreach遍歷有相同屬性的值之前,我們需要先了解幾…

    編程 2025-04-28
  • Python strip()函數的功能和用法用法介紹

    Python的strip()函數用於刪除字符串開頭和結尾的空格,包括\n、\t等字符。本篇文章將從用法、功能以及與其他函數的比較等多個方面對strip()函數進行詳細講解。 一、基…

    編程 2025-04-28

發表回復

登錄後才能評論