Editing XML Documents with Internet Explorer

You can alter the contents of an XML document in Internet Explorer. To do this, you use methods such as createElement , insertBefore , createTextNode , and appendChild .

As an example, I'll alter the document ch07_01.xml by inserting a new element, <MEETING_CHAIR> , like this:

 <?xml version="1.0"?>  <MEETINGS>    <MEETING TYPE="informal">  <MEETING_CHAIR>Ted Bond</MEETING_CHAIR>  <MEETING_TITLE>XML In The Real World</MEETING_TITLE>        <MEETING_NUMBER>2079</MEETING_NUMBER>        <SUBJECT>XML</SUBJECT>        <DATE>6/1/2003</DATE>        <PEOPLE>            <PERSON ATTENDANCE="present">                <FIRST_NAME>Edward</FIRST_NAME>                <LAST_NAME>Samson</LAST_NAME>            </PERSON>             .             .             . 

I begin by creating the new node, corresponding to the <MEETING_CHAIR> element, and inserting it into the document with the insertBefore method:

 <HTML>      <HEAD>         <XML ID="meetingsXML" SRC="ch07_01.xml"></XML>         <SCRIPT LANGUAGE="JavaScript">         <!--             function alterDocument()             {                 var xmldoc, rootNode, meetingsNode, meetingNode, createdNode, graphics/ccc.gif createdTextNode                 xmldoc = document.all.meetingsXML                 rootNode = xmldoc.documentElement                 meetingsNode = rootNode.firstChild                 meetingNode = meetingsNode.firstChild  createdNode = xmldoc.createElement("MEETING_CHAIR")   createdNode = meetingsNode.insertBefore(createdNode, meetingNode)  .                 .                 . 

Now I will create the text node inside this new element. The text node will hold the text "Ted Bond" . I'll create it with the createTextNode method and append it to the <MEETING_CHAIR> element with the appendChild method:

 <HTML>      <HEAD>         <XML ID="meetingsXML" SRC="ch07_01.xml"></XML>         <SCRIPT LANGUAGE="JavaScript">         <!--             function alterDocument()             {                 var xmldoc, rootNode, meetingsNode, meetingNode, createdNode, graphics/ccc.gif createdTextNode                 xmldoc = document.all.meetingsXML                 rootNode = xmldoc.documentElement                 meetingsNode = rootNode.firstChild                 meetingNode = meetingsNode.firstChild                 createdNode = xmldoc.createElement("MEETING_CHAIR")                 createdNode = meetingsNode.insertBefore(createdNode, meetingNode)  createdTextNode = xmldoc.createTextNode("Ted Bond")   createdNode.appendChild(createdTextNode)  .                 .                 . 

Now I've altered the documentbut at this point, it exists only inside the xmldoc object. How do I display it in the browser? The DOMDocument object actually has a save method that allows you to save the document to a new file like this: xmldoc.save("new.xml") . However, you can't use that method without changing the security settings in Internet Explorer because, by default, browsers aren't supposed to be capable of writing files on the host machine.

I'll take a different approach. In this case, I'll store the XML document's text in a hidden control in an HTML form (a hidden control simply holds text invisible to the user ), and I'll send the data in that form to a server-side Active Server Pages (ASP) script. That script will just echo the document back to the browser, which, in turn , will display it. Here's the ASP script, ch07_14.asp, where I set the MIME type of this document to "text/xml" , add an <?xml?> processing instruction, and echo the XML data back to Internet Explorer (ASP scripts like this one are beyond the scope of this book, but we'll take a look at them in brief in Chapter 20, "WML, ASP, JSP, Servlets, and Perl"):

Listing ch07_14.asp
 <%@ LANGUAGE="VBSCRIPT" %> <% Response.ContentType = "text/xml" Response.Write "<?xml version=" & Chr(34) & "1.0" & Chr(34) & "?>" & Chr(13) & Chr(10) Response.Write Request("data") %> 

I have an ASP server on my host machine (Microsoft Internet Information Server, IIS), so the URI I'll send the XML document to is http://steve/ch07_14.asp . I do that by using the HTML form's submit method (which works exactly as if the user had clicked a Submit button in the form) after loading the XML document into the page's hidden control:

Listing ch07_15.html
 <HTML>     <HEAD>         <XML ID="meetingsXML" SRC="ch07_01.xml"></XML>         <SCRIPT LANGUAGE="JavaScript">         <!--             function alterDocument()             {                 var xmldoc, rootNode, meetingsNode, meetingNode, createdNode, graphics/ccc.gif createdTextNode                 xmldoc = document.all.meetingsXML                 rootNode = xmldoc.documentElement                 meetingsNode = rootNode.firstChild                 meetingNode = meetingsNode.firstChild                 createdNode = xmldoc.createElement("MEETING_CHAIR")                 createdNode = meetingsNode.insertBefore(createdNode, meetingNode)                 createdTextNode = xmldoc.createTextNode("Ted Bond")                 createdNode.appendChild(createdTextNode)                 document.all.data.value = meetingsXML.documentElement.xml  document.form1.submit()  }         //-->         </SCRIPT>     </HEAD>     <BODY>         <CENTER>             <FORM NAME="form1" ACTION="http://steve/ch07_14.asp" METHOD="POST">             <INPUT TYPE="HIDDEN" NAME="data">             <INPUT TYPE="BUTTON" VALUE="Alter the document" onclick="alterDocument()">             </FORM>         </CENTER>     </BODY> </HTML> 

And that's it. Now when the user clicks the button with the caption Alter the Document, the code in this page alters the XML document and sends it to the server. The ASP script on the server echoes the XML document back to the browser, which displays it, as you see in Figure 7-10. You can see the new <MEETING_CHAIR> element in that figure.

Figure 7-10. Altering an XML document in Internet Explorer.

graphics/07fig10.gif

We've put JavaScript to work in this chapter, parsing and accessing XML documents. In the next chapter, I'm going to put JavaScript to work treating XML data as database objects, and I'll turn to that now.



Real World XML
Real World XML (2nd Edition)
ISBN: 0735712867
EAN: 2147483647
Year: 2005
Pages: 440
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net