Jump to content

Adding new Elements with children


ameliabob

Recommended Posts

In your "bookstore" example in the tutorials, If I wanted to add a new book, should I add the element <book> by itself as in bStore = loadXLMdoc("books.xml");startPlace = bStore.getElementsbyTagName("bookstore");newBook = createElement("book");bStore.appendChild(newBook);newStart = bStore.lastChild;newBook = createElement("book");bStore.body.appendChild(newBook);and then go back and add the piecescreateElement("title");createTextNode("XML Crash Course") OR build the book and add it as ....bStore = loadXLMdoc("books.xml");newBook = createElement("book");newTitle = newbook.addChild("title");text = newTitle.createTextNode("XML crash Course");newTitle.appendChild(text);newBook.appendChild(newTitle);nextTitle = newBook.addChild("author");nextText = nextTitle.createTextNode("someone nice");nextTitle = appendChild(nexttext);newBook.appendChild(nextTitle);bStore.body.appendChild(newBook);

Link to comment
Share on other sites

I've never even heared about the addChild() method (I prefer document.createElement()), but you should build everything you want to add, and then add it, i.e. build from the insides out.For example:

newBook = document.createElement("book");newTitle = document.createElement("title", "XML crash Course");newAuthor = document.createElement("author", "someone nice");newBook.appendChld(newTitle);newBook.appendChild(newAuthor);bStore.getElementsByTagName("body")[0].appendChild(newBook);

This particular example could actually be reduced to

newBook = document.createElement("book");newBook.appendChld(document.createElement("title", "XML crash Course"));newBook.appendChild(document.createElement("author", "someone nice"));bStore.getElementsByTagName("body")[0].appendChild(newBook);

But the first way is probably more readable.

Link to comment
Share on other sites

In your "bookstore" example in the tutorials, If I wanted to add a new book, should I add the element <book> by itself as in bStore = loadXLMdoc("books.xml");startPlace = bStore.getElementsbyTagName("bookstore");newBook = createElement("book");bStore.appendChild(newBook);newStart = bStore.lastChild;newBook = createElement("book");bStore.body.appendChild(newBook);and then go back and add the piecescreateElement("title");createTextNode("XML Crash Course") OR build the book and add it as ....bStore = loadXLMdoc("books.xml");newBook = createElement("book");newTitle = newbook.addChild("title");text = newTitle.createTextNode("XML crash Course");newTitle.appendChild(text);newBook.appendChild(newTitle);nextTitle = newBook.addChild("author");nextText = nextTitle.createTextNode("someone nice");nextTitle = appendChild(nexttext);newBook.appendChild(nextTitle);bStore.body.appendChild(newBook);
Link to comment
Share on other sites

Super! That got me off that hurdle. And as always adds more questions. The keywords got entered as capital letters "author" as "AUTHOR". Why did this happen? and secondly the second parameter didn't get added as the text. Why was this?

Link to comment
Share on other sites

The first may be a bug in the browser you're using. For some reason, it doesn't see the document as an XML one, but as HTML one (and HTML DOM elements are by convention written in uppercase).The second appears to be a lack of imlpementation, and actually appears in most browsers. Sorry, I missed that part, as I'm most used to using DOM in PHP, where there is support for this.The nodeValue object of the DOMElement object seems to be supported everywhere, so use that instead:

newBook = document.createElement("book");newTitle = document.createElement("title");newTitle.nodeValue = "XML crash Course";newAuthor = document.createElement("author");newAuthor.nodeValue = "someone nice";newBook.appendChld(newTitle);newBook.appendChild(newAuthor);bStore.getElementsByTagName("body")[0].appendChild(newBook);

If that too doesn't work, then the last solution is to use innerHTML instead. innerHTML is a proprietary (i.e. not a DOM standard) object that despite not being a standard is implemented in all browsers. It allows embedding of raw HTML at that certain point. Different browsers handle HTML inputted with this object differently, but plain text is always rendered the same, so it's safe to use innerHTML as a nodeValue replacement, like so:

newBook = document.createElement("book");newTitle = document.createElement("title");newTitle.innerHTML = "XML crash Course";newAuthor = document.createElement("author");newAuthor.innerHTML = "someone nice";newBook.appendChld(newTitle);newBook.appendChild(newAuthor);bStore.getElementsByTagName("body")[0].appendChild(newBook);

Link to comment
Share on other sites

I've never even heared about the addChild() method (I prefer document.createElement()), but you should build everything you want to add, and then add it, i.e. build from the insides out.For example:
newBook = document.createElement("book");newTitle = document.createElement("title", "XML crash Course");newAuthor = document.createElement("author", "someone nice");newBook.appendChld(newTitle);newBook.appendChild(newAuthor);bStore.getElementsByTagName("body")[0].appendChild(newBook);

This particular example could actually be reduced to

newBook = document.createElement("book");newBook.appendChld(document.createElement("title", "XML crash Course"));newBook.appendChild(document.createElement("author", "someone nice"));bStore.getElementsByTagName("body")[0].appendChild(newBook);

But the first way is probably more readable.

Link to comment
Share on other sites

I have tried the IE way of displaying the xml file but when I use

<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 />");document.write("<xmp>" + xmlDoc.xml + "</xmp>");x.insertBefore(newNode,y[3]);y=xmlDoc.getElementsByTagName("book");document.write("Book elements after: " + y.length);// BBBdocument.write("<xmp>" + xmlDoc.xml + "</xmp>");</script></body></html>

the line of code after the BBB doesn't show the new Element. Why is this and what to I do to see it?

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...