我有一个units.xml
文件,看起来像这样
<dbms>
<units>
<UNITID>1</UNITID>
<TYPE>Administrative Office</TYPE>
<UNITINITIALS>OAA</UNITINITIALS>
<UNITNAME>Office of Alumni Affairs</UNITNAME>
</units>
<units>
.... and so on
</dbms>
我想在节点上附加一个新元素LOCATION
,我使用此代码:
<script>
var xmlDoc=loadXMLDoc("units.xml");
x=xmlDoc.getElementsByTagName('units');
x[0].setAttribute("LOCATION","Ground Floor");
document.write("Location: ");
document.write(x[0].getAttribute("LOCATION"));
</script>
它打印出我想要的内容(“Location: Ground Floor
”)但是当我打开units.xml
文件时,没有任何改变。我做错了吗?
答案 0 :(得分:0)
Java脚本不是服务器端语言,但php可以处理
<?php
define("UPLOADEDFILES", " url of units.xml ");
$doc = new DOMDocument('1.0');
$xmlfile=UPLOADEDFILES."units.xml";
if(is_file($xmlfile)){ // test if file exists
$doc->load($xmlfile);
$Dbms = $doc->getElementsByTagName("dbms");
$root = $Dbms->item(0);
} else{ // if file does not exists
$root = $doc->createElement('dbms');
$doc->appendChild($root);
$UNITS = $doc->createElement("units");
$root->appendChild($UNITS);
}
$units=$root->getElementsByTagName("units")->item(0);
$units->setAttribute("LOCATION","blablabla");//append the attribute LOCATION to the first units node
//you can append the attribute wherever you want , just follow the previous example
$doc->save($xmlfile); // save changes
?>