Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 8th, 2003, 12:01 PM
Authorized User
 
Join Date: Sep 2003
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to generate a text file?

Hi All, I have a client side application. User can enter value(s) and when the submit button is clicked, it will display at the bottom of the html page. However, I also want it to send the values to a text file. How should I do that? I want the values to display as follows:-

Book Title Publisher's name Published Date
Pages ISBN Price
Abstract
Author
Author
Author
Author
Author
Category
Category
Category


My client side application code (it works) as follows:-
================================================== =======

<html>
<head>
<title>Wrox Press book data entry page</title>

<body onload="initializeBook()" >
    <h1>Wrox Press book data entry page</h1>
    <h3>Book information:</h3>
    <table>
         <tr>
              <td>Title:</td>
                       <td><input id="txtTitle"></input></td>
         </tr>
         <tr>
              <td>Publisher:</td><td><input id="txtPublisher"></input></td>
         </tr>
         <tr>
              <td>Published Date:</td><td><input id="txtPubDate"></input></td>
         </tr>
         <tr>
               <td>Abstract:</td><td><input id="txtAbstract"></input></td>
         </tr>
         <tr>
              <td>Pages:</td><td><input id="txtPages"></input></td>
          </tr>
         <tr>
              <td>ISBN:</td><td><input id="txtISBN"></input></td>
         </tr>
         <tr>
              <td>Price:</td><td><input id="txtPrice"></input></td>
         </tr>
    </table>
    <input id="btnUpdate" type="button" value="Update book info" onclick="updateBookInfo()"></input>
    <h3>Authors:</h3>
    <table>
         <tr>
              <td>Author:</td><td><input id="txtAuthor"></input></td>
         </tr>
    </table>
    <input id="btnAddAuthor" type="button" value="Add author" onclick="addAuthor()"></input>

     <h3>Categories:</h3>
    <table>
         <tr>
              <td>Category:</td><td><input id="txtCategory"></input></td>
         </tr>
    </table>
    <input id="btnAddCategory" type="button" value="Add Category" onclick="addCategory()"></input>

    <xml id="docBook">
         <book>
         </book>
    </xml>

    <script>

    var docBook;

    function initializeBook()
    {
         docBook = document.all("docBook").XMLDocument;
         docBook.async = "false";
         renderElements();
     }

     function createOrReplaceElement(sElementName, sElementValue, elementParent)
    {
         var elementItem;
         var textValue;
         var nodelistOldItem;

         elementItem = docBook.createElement(sElementName);
         textValue = docBook.createTextNode(sElementValue);
         elementItem.appendChild(textValue);

         nodelistOldItem = elementParent.getElementsByTagName(sElementName);
         if (nodelistOldItem.length > 0)
         {
              elementParent.replaceChild(elementItem, nodelistOldItem.item(0));
         }
         else
         {
              elementParent.appendChild(elementItem);
         }
    }
    function updateBookInfo()
    {
         createOrReplaceElement("Title", txtTitle.value, docBook.documentElement);
         createOrReplaceElement("Publisher", txtPublisher.value, docBook.documentElement);
         createOrReplaceElement("PubDate", txtPubDate.value, docBook.documentElement);
         createOrReplaceElement("Abstract", txtAbstract.value, docBook.documentElement);
         createOrReplaceElement("Pages", txtPages.value, docBook.documentElement);
         createOrReplaceElement("ISBN", txtISBN.value, docBook.documentElement);
         createOrReplaceElement("Price", txtPrice.value, docBook.documentElement);

         renderElements();
    }
    function addAuthor()
    {
         var elementAuthor;
         var textAuthor;
         var nodelistAuthors;
         var elementAuthors;
          elementAuthor = docBook.createElement("author");
         textAuthor = docBook.createTextNode(txtAuthor.value);
         elementAuthor.appendChild(textAuthor);
         nodelistAuthors = docBook.getElementsByTagName("authors");
         if (nodelistAuthors.length == 0)
         {
              elementAuthors = docBook.createElement("authors");
              docBook.documentElement.appendChild(elementAuthors );
         }
         else
         {
              elementAuthors = nodelistAuthors.item(0);
         }
         elementAuthors.appendChild(elementAuthor);
         renderElements();
    }
    function addCategory()
    {
         var elementCategory;
         var textCategory;
         var nodelistRecSubjCategories;
         var elementRecSubjCategories;
          elementCategory = docBook.createElement("category");
         textCategory = docBook.createTextNode(txtCategory.value);
         elementCategory.appendChild(textCategory);
         nodelistRecSubjCategories = docBook.getElementsByTagName("recSubjCategories");
         if (nodelistRecSubjCategories.length == 0)
         {
              elementRecSubjCategories = docBook.createElement("recSubjCategories");
              docBook.documentElement.appendChild(elementRecSubj Categories);
         }
         else
         {
              elementRecSubjCategories = nodelistRecSubjCategories.item(0);
         }
         elementRecSubjCategories.appendChild(elementCatego ry);
         renderElements();
    }
    function renderElements()
    {
         document.all("divRawXML").innerText = docBook.xml;
         bookInfo.innerHTML = docBook.transformNode(bookXSL.documentElement);
         authorTable.innerHTML = docBook.transformNode(authorXSL.documentElement);
         categoryTable.innerHTML = docBook.transformNode(categoryXSL.documentElement) ;
    }
    </script>

  <xml id="bookXSL">
       <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
           <xsl:output method="html" version="1.0" indent="yes"/>
               <xsl:template match="/">
                     <xsl:choose>
                          <xsl:when test="/book/Title[. != '']">
                               <table border="0" cellpadding="1">
                                    <tr>
                                         <td>Title:</td>
                                         <td><xsl:value-of select="/book/Title"/></td>
                                     </tr>
                                    <tr>
                                         <td>Publisher:</td>
                                         <td><xsl:value-of select="/book/Publisher"/></td>
                                    </tr>
                                    <tr>
                                         <td>Published Date:</td>
                                         <td><xsl:value-of select="/book/PubDate"/></td>
                                    </tr>
                                    <tr>
                                         <td>Abstract:</td>
                                         <td><xsl:value-of select="/book/Abstract"/></td>
                                    </tr>
                                    <tr>
                                         <td>Pages:</td>
                                         <td><xsl:value-of select="/book/Pages"/></td>
                                    </tr>
                                    <tr>
                                         <td>ISBN:</td>
                                         <td><xsl:value-of select="/book/ISBN"/></td>
                                    </tr>
                                    <tr>
                                         <td>Price:</td>
                                         <td><xsl:value-of select="/book/Price"/></td>
                                    </tr>
                               </table>
                          </xsl:when>
                          <xsl:otherwise>
                               <p>Book Information not yet specified.</p>
                          </xsl:otherwise>
                     </xsl:choose>
               </xsl:template>
       </xsl:stylesheet>
   </xml>

    <xml id="authorXSL">
         <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
           <xsl:output method="html" version="1.0" indent="yes"/>
               <xsl:template match="/">
          <table border="0" cellpadding="1">
              <tr>
                   <td><strong>Authors</strong></td>
              </tr>
              <xsl:for-each select="/book/authors/author">
                   <tr>
                        <td><xsl:value-of select="text()"/></td>
                   </tr>
              </xsl:for-each>
         </table>
         </xsl:template>
       </xsl:stylesheet>
    </xml>

    <xml id="categoryXSL">
        <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
           <xsl:output method="html" version="1.0" indent="yes"/>
               <xsl:template match="/">
           <table border="0" cellpadding="1">
              <tr>
                   <td><strong>Categories</strong></td>
              </tr>
               <xsl:for-each select="/book/recSubjCategories/category">
                   <tr>
                        <td><xsl:value-of select="text()"/></td>
                   </tr>
              </xsl:for-each>
         </table>
         </xsl:template>
       </xsl:stylesheet>
    </xml>


    <h2>Book information</h2>
    <p><div id="bookInfo"></div></p>
    <p><div id="authorTable"></div></p>
    <p><div id="categoryTable"></div></p>
</hr>
    The text expression of the current contents of the DOM tree is:
    <pre><div id="divRawXML"></div></pre>
</body>
</head>
</html>

 
Old October 9th, 2003, 03:48 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You won't be able to save a text file to the client without warnings, it's too dangerous. To output text you need to have the stylesheet's output method as text and then call the DomDocument.save method providing the path. Does that answer your question or did you have something else in mind?

Joe (MVP - xml)
 
Old October 9th, 2003, 10:51 AM
Authorized User
 
Join Date: Sep 2003
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Joe, thanks for your reply. I have few questions.

1) I posted the question because I actually need to read a text file which already submitted to server after the user enter values in client side application. How to save the values into a text file to server side? If using stylesheet as you mentioned, do I need to save it in seperate file (like filename.xsl)?

2) If I want to send the values to text file from Client side ... you were saying it's too dangerous, right? That means, we can't do that from client side application?

Thank you very much, I really apprecaite that. As I am so new in this XML and I got confused sometimes about all the stylesheets ... CSS, XSL, XSLT ... etc! :(





Similar Threads
Thread Thread Starter Forum Replies Last Post
Using XSLT to generate PDF with Japanese text XSLTUser XSLT 5 December 20th, 2007 09:01 PM
auto generate & save text file marcusbris Visual Basic 2005 Basics 0 September 5th, 2007 01:43 AM
adding lines after generate xsl file kgoldvas XSLT 3 July 16th, 2007 03:20 AM
Generate a summary from an XML file. Missing_Piece XSLT 1 May 2nd, 2006 04:58 AM
how to generate a text file using .NET? pooh2323 VS.NET 2002/2003 2 February 11th, 2004 11:30 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.