2011-07-10 12 views
0

ゴール:
htmlドキュメントのXML domに基づいてxmlファイルに新しいデータを追加できるようにするには。xmlファイルにデータを保存できません

問題:
このコードはw3schoolから取得され、xmlにデータを保存しようとしましたが、ソースコードの実行後に何も保存されません。

コードは、「http://www.w3schools.com/Dom/dom_nodes_add.asp」 - 「ノードを挿入する - insertBefore()」から取得します。

// Fullmetalboy


<html> 
<head> 
<script type="text/javascript" src="loadxmldoc.js"> 
</script> 
</head> 
<body> 

<script type="text/javascript"> 
xmlDoc=loadXMLDoc("books.xml"); 

newNode=xmlDoc.createElement("book"); 

x=xmlDoc.documentElement; 
y=xmlDoc.getElementsByTagName("book"); 

document.write("Book elements before: " + y.length); 
document.write("<br />"); 
x.insertBefore(newNode,y[3]); 

y=xmlDoc.getElementsByTagName("book"); 
document.write("Book elements after: " + y.length); 
</script> 
</body> 
</html> 

function loadXMLDoc(dname) 
{ 
    if (window.XMLHttpRequest) 
    { 
     xhttp=new XMLHttpRequest(); 
    } 
    else 
    { 
     xhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 

    xhttp.open("GET",dname,false); 
    xhttp.send(); 
    return xhttp.responseXML; 
} 

<?xml version="1.0" encoding="ISO-8859-1"?> 
<!-- Edited by XMLSpy® --> 
<bookstore> 
<book category="cooking"> 
<title lang="en">Everyday Italian</title> 
<author>Giada De Laurentiis</author> 
<year>2005</year> 
<price>30.00</price> 
</book> 
<book category="children"> 
<title lang="en">Harry Potter</title> 
<author>J K. Rowling</author> 
<year>2005</year> 
<price>29.99</price> 
</book> 
<book category="web"> 
<title lang="en">XQuery Kick Start</title> 
<author>James McGovern</author> 
<author>Per Bothner</author> 
<author>Kurt Cagle</author> 
<author>James Linn</author> 
<author>Vaidyanathan Nagarajan</author> 
<year>2003</year> 
<price>49.99</price> 
</book> 
<book category="web" cover="paperback"> 
<title lang="en">Learning XML</title> 
<author>Erik T. Ray</author> 
<year>2003</year> 
<price>39.95</price> 
</book> 
</bookstore> 
+0

この問題を解決できましたか?私もこの時点で立ち往生しています。 – RRForUI

答えて

0

代わりの

x.insertBefore(newNode,y[3]); 

試用版:

xmlDoc.documentElement.insertBefore(newNode, y[3]); 
関連する問題