本文目錄一覽:
- 1、PHP添加XML內容
- 2、PHP XML刪除的問題
- 3、在線等 PHP如何操作XML
- 4、求PHP對XML增加刪除修改的方法或者類
- 5、PHP操作xml的問題,如何插入數據
- 6、請教,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’)) {
//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-tw/n/152531.html