phpxml刪除節點
㈠ php刪除xml文件根節點以外的所有節點怎麼寫
刪除所有的節點,添加根節點
㈡ xml刪除一個節點
刪除指定的XML節點
呵呵,今天總算有點成績了。明白了如何添加XML節點,現在又搞懂了如何刪除XML節點。下面說一下具體的方法:
下面是XML文檔:
<?xml version="1.0" encoding="gb2312"?>
<data>
<list>
<url>cns!1pg3-qZraLhileWMaX48lbhg!246.entry</url>
<title>試用 FCKeditor 2.0 RC3</title>
<rq>6.13</rq>
</list>
<list>
<url>cns!1pg3-qZraLhileWMaX48lbhg!287.entry</url>
<title>比爾蓋茨給馬化騰的一封信</title>
<rq>6.6</rq>
</list>
<list>
<url>cns!1pg3-qZraLhileWMaX48lbhg!282.entry</url>
<title>溜之大吉 v1.0</title>
<rq>6.2</rq>
</list>
<list>
<url>cns!1pg3-qZraLhileWMaX48lbhg!281.entry</url>
<title>一個女孩兒的自戀</title>
<rq>6.1</rq>
</list>
</data>
要想刪除指定的XML節點,必須要有指定的刪除條件。在這兒,我選擇二項:title、rq。
下面是實現過程(C#代碼):
<% @ Page Language="c#" %>
<% @ Import Namespace="System.Xml" %>
<Script Language="C#" Runat="Server">
void Page_Load(){
string theTitle="試用 FCKeditor 2.0 RC3";
string theRq="6.13";
// 創建一個XmlDocument對象
string xmlPath=Server.MapPath("blog.xml");
XmlDocument xmlDoc=new XmlDocument();
// 載入指定的XML數據
xmlDoc.Load(xmlPath);
// 選擇匹配 XPath 表達式的第一個 XmlNode
XmlNode root=xmlDoc.SelectSingleNode("data");
// 獲取節點的所有子節點
XmlNodeList xnList=root.ChildNodes;
bool isFinded=false;
foreach(XmlNode xn in xnList){
if(xn.SelectSingleNode("title").InnerText==theTitle && xn.SelectSingleNode("rq").InnerText==theRq){
isFinded=true;
root.RemoveChild(xn);// 移除指定的子節點
xmlDoc.Save(xmlPath);
break;
}
}
// 顯示結果
if(isFinded)
Response.Write ("已刪除");
else
Response.Write ("未找到相應的記錄");
}
</Script>
㈢ 如何刪除xml中結點的某個屬性
刪除屬性
1 public void DeleteAttribute(string xmlPath)
2 {
3 XmlDocument xmlDoc = new XmlDocument();
4 xmlDoc.Load(xmlPath);
5 XmlElement node = (XmlElement)xmlDoc.SelectSingleNode("BookStore/NewBook");
6 //移除指定屬性
7 node.RemoveAttribute("Name");
8 //移除當前節點所有屬性,不包括默認屬性
9 //node.RemoveAllAttributes();
10 xmlDoc.Save(xmlPath);
11 }
㈣ PHP+XML怎樣刪除多重節點
用jdom或者dom4j,可以很輕松的通過調用他們的API來達到你的要求
㈤ php刪除指定xml
你試試把$_GET[tu_id]強制轉換試試
(int)$_GET["tu_id"]
㈥ C#中如何刪除XML文件中的某個節點
假設有如下XML文件:
<?xmlversion="1.0"encoding="utf-8"?>
<books>
<bookname="失蹤的孩子"isbn="9787020139927"author="[意]埃萊娜·費蘭特"/>
<bookname="我要快樂,不必正常"isbn="9787559614988"author="[英]珍妮特·溫特森"/>
</books>
如下代碼可移除name為"我要快樂,不必正常"的節點:
stringfilePath="D:\XMLFile1.xml";
//創建一個XML文檔對象
XmlDocumentdocument=newXmlDocument();
//載入文檔
document.Load(filePath);
//獲取所有book節點
XmlNodeListnodes=document.GetElementsByTagName("book");
//遍歷節點,如果節點的name屬性為"我要快樂,不必正常",則從父節點中刪除
//這里之所以從後向前遍歷,是為了處理刪除節點之後節點集合變化的問題
for(inti=nodes.Count-1;i>=0;i--)
{
XmlNodenode=nodes[i];
if(node.Attributes["name"]!=null
&&node.Attributes["name"].Value=="我要快樂,不必正常")
{
node.ParentNode.RemoveChild(node);
}
}
//刪除節點之後,保存文檔;
document.Save(filePath);
運行之後文檔變為:
㈦ 對已經有的xml文件添加刪除節點怎麼操作
沒接觸過對xml的操作...
既然是在頁面上操作,就是對DOM節點的操作;
$(obj).remove(); //這是移除對象節點
$(obj).html('xxxxx'); $(obj).append(); 等方法也都可以對dom結構進行更改操作。
如果是我所說的dom節點操作,你可以看下jquery手冊裡面的「dom處理」,如果不是我也不太清楚了。
㈧ 如何用最簡單的方法刪除xml文件下指定節點的全部內容
InputStream is = new FileInputStream("D://dom4j.xml");
SAXReader reader = new SAXReader();
Document doc = reader.read(is);
doc.normalize();
Node node = doc.selectSingleNode("/datas/*");//查找datas下的第一個子結點的內空
node.getParent().remove(node);
OutputFormat outformat = OutputFormat.createPrettyPrint();
// xml自身的編碼
outformat.setEncoding(doc.getXMLEncoding());
outformat.setIndent(true);
File targetFile = new File("D:/dom4j3.xml");
// 將xmlStr輸出到目標路徑,使用xml文件的頭文件編碼
OutputStream out = new FileOutputStream(targetFile);
XMLWriter writer = new XMLWriter(out, outformat);
// 是否轉義特殊字元,默認true轉義,false表示不轉義
writer.setEscapeText(false);
writer.write(doc);
writer.flush();