How do I add (or insert) a node into an XML document with JSTL?

Dear all,
I am trying to use JSTL to store some data from a form into XML. However, I have read many articles in JSTL and only found that it can only be, as far as those articles are concerned, used to "read" data from XML files, but not "write" to them.
Am I missing something in JSTL? Or, if I want to write to an XML file (e.g., insert a new node, or modify a node's data), will I have to write my own bean and then use <c:usebean />?
Thanks very much in advance.
Regards,
Robert

JSTL is not a programming language in and of itself. It is intended to help you to write scriptlet free JSPs.
JSP pages (by one school of thought) are intended to function as the "View" in a Model-View-Controller architecture.
Being a view, JSP pages should be able to look at things - but not touch.
The standard pattern is to go to a servlet first, which invokes the logic, and then forwards to a JSP for rendering.
Editing XML by adding new nodes etc etc is thus a function of the java code layer - servlet/action whatever you want to call it.
Giving you the power to edit XML documents via JSTL (ie on a JSP page) takes away from purity of the MVC design.
Hence why they don't provide the tags for manipulating XML, and only provide the database tags with a disclaimer that they should only be used for prototypes/trivial apps!
JSTL is designed to replace scriptlet code on a JSP page.
It can replace 99% of scriptlet code. The other 1% shouldn't be on the JSP in the first place :-)
cheers,
evnafets

Similar Messages

  • Inserting an element into an XML document

    I am simply looking insert a new element into an existing XML Document using JDOM. Here is my code so far:
    public class UserDocumentWriter {
         private SAXBuilder builder;
         private Document document;
         private File file = new File("/path/to/file/users.xml");
         private Element rootElement;
         public UserDocumentWriter() {
              builder = new SAXBuilder();
              try {
                   document = builder.build(file);
                   rootElement = document.getRootElement();
              } catch (IOException ioe) {
                   System.err.println("ERROR: Could not build the XML file.");
              } catch (JDOMException jde) {
                   System.err.println("ERROR: " + file.toString() + " is not well-formed.");
         public void addContact(String address, String contact) {
              List contactList = null;
              Element contactListElement;
              Element newContactElement = new Element("contact");
              newContactElement.setText(contact);
              List rootsChildren = rootElement.getChildren();
              Iterator iterator = rootsChildren.iterator();
              while (iterator.hasNext()) {
                   Element e = (Element) iterator.next();
                   if (e.getAttributeValue("address").equals(address)) {
                        contactListElement = e.getChild("contactlist");
                        contactListElement.addContent(newContactElement);
                        writeDocument(document);
         public void writeDocument(Document doc) {
              try {
                   XMLOutputter output = new XMLOutputter();
                   OutputStream out = new FileOutputStream(file);
                   output.output(doc, out);
              } catch (FileNotFoundException ntfe) {
                   System.err.println("ERROR: Output file not found.");
              } catch (IOException ioe) {
                   System.err.println("Could not output document changes.");
         }However, the problem is, the newly added element will always be appended to the end of the last line, resulting in the following:
    <contactlist>
                <contact>[email protected]</contact><contact>[email protected]</contact></contactlist>Is there anyway in which I can have the newly added element create it's own line for the purpose of tidy XML? Alternatively is there a better methodology to do the above entirely?

    Your question is not very clear.
    Do you want to know How to insert an element into an XML document?
    Answer: I can see you already know how to do it. You have added the element using addContent()
    or do you want to know How to display the XML in a tidy format?
    Answer: to view the XML in a properly formatted style you can you the Format class. A very basic way of viewing the XML would be:
       * Prints the Document to the specified file.
       * @param doc
       * @param filename
       * @param formatting
      public static void printDocToFile(Document doc, String strFileName,
          boolean formatting)
        XMLOutputter xmlOut = null;
        if (!formatting)
          xmlOut = new XMLOutputter();
        } else
          Format prettyFormat = Format.getPrettyFormat();
          prettyFormat.setOmitEncoding(false);
          prettyFormat.setOmitDeclaration(false);
          xmlOut = new XMLOutputter(prettyFormat);
        try
          if (doc != null)
            FileWriter writer = new java.io.FileWriter(strFileName, true);
            xmlOut.output(doc, writer);
            writer.flush();
            writer.close();
          } else
            System.out.println("Document is null.");
        catch (Exception ex)
          System.out.println(ex);
      }

  • How can I use DOM to copy Nodes between different XML Document ?

    Can I copy one Node or Element from one XML Document to another Document by DOM ?
    Because I use the Xerces , but it is not work if I want to copy Node between different Document, or I am doing something wrong. Anyone can help me , I will deeply appreciated !!

    check out the importNode() method of Document.

  • How to add a new node into existing XML Document

    I have a very simple question. I use XML as input argument for PL/SQL procedure that inserts data into the corresponding table. All I have to do is to add a new tag for Primary Key column and put sequence.NEXTVAL - value into it.
    <ROWSET>
    <ROW>
    -- Add <ID_table_name> value </ID_table_name> ??????
    <FIELD1>data1</FIELD1>
    <FIELD2>data1</FIELD2>
    </ROW>
    </ROWSET>
    I've parsed XML, but I couldn't find the way how to insert the new NODE.
    If you know how to use packages XMLDOM, XMLParser for this purpose, please help me!
    Oracle version 8.1.7

    DOMParser parser=new DOMParser();
    XMLDocument xmlDocument=parser.getdocument();
    Node node=xmlDocument.selectSingleNode("/ROWSET/ROW");
    Element element=xmlDocument.createElement(String tagName)
    node.appendChild(element);

  • Inserting PI into generated XML document

    Can anyone tell me how to insert processing instructions into an XML document generated using the xmlcg generated classes? I understand that I can create a PI using the Document::createProcessingInstruction() method but how do I add it as a sibling to the root element? Any insight in this regard will be really appreciated.

    Your question is not very clear.
    Do you want to know How to insert an element into an XML document?
    Answer: I can see you already know how to do it. You have added the element using addContent()
    or do you want to know How to display the XML in a tidy format?
    Answer: to view the XML in a properly formatted style you can you the Format class. A very basic way of viewing the XML would be:
       * Prints the Document to the specified file.
       * @param doc
       * @param filename
       * @param formatting
      public static void printDocToFile(Document doc, String strFileName,
          boolean formatting)
        XMLOutputter xmlOut = null;
        if (!formatting)
          xmlOut = new XMLOutputter();
        } else
          Format prettyFormat = Format.getPrettyFormat();
          prettyFormat.setOmitEncoding(false);
          prettyFormat.setOmitDeclaration(false);
          xmlOut = new XMLOutputter(prettyFormat);
        try
          if (doc != null)
            FileWriter writer = new java.io.FileWriter(strFileName, true);
            xmlOut.output(doc, writer);
            writer.flush();
            writer.close();
          } else
            System.out.println("Document is null.");
        catch (Exception ex)
          System.out.println(ex);
      }

  • How do I add scripts in order to make the page compatible with all the browsers.

    How do I add scripts in order to make the page compatible with all the browsers.

    As Jon says, please be more specific about "don't line up right"
    I notice you are inserting some arbitrary <BR> tags which will cause very unpredictable displays with varying user viewports and text preferences.
    <P>In 2008-2009, Families Forward benefited 278 homeless Philadelphia families, <BR>
      including 834 children. Families Forward assisted 1,413 stranded travelers, including <BR>
      235 children. In the first 6 months of 2009-2010, from July 1, 2009 to <BR>
      January 1, 2010, Families Forward assisted 185 homeless families and 220 <BR>
      stranded travelers.</P>

  • How do I insert multiple photos into an excel document vs inserting each individually?  Insert, Picture, From FIle...now what?  it only allows me to choose 1 at a time.

    Help - How do I insert multiple photos into an excel document vs inserting each individually?  Insert, Picture, From FIle...now what?  it only allows me to choose 1 at a time.

    https://discussions.apple.com/thread/3383532?tstart=0
    Stefan

  • How to delete a node in an XML document

    Hi,
    My flex plugin loads an XML document that is passed in from
    the ExternalInterace. I have a List control that displays data from
    this XML document.
    The XML looks like:
    <document>
    <response>
    <objectlist>
    <list type="typeValue">
    <object type="typeValue" name="Name Value" id="GUID
    Value1">
    <params>
    <params>
    </object>
    <!-- more objects in this list -->
    </list>
    </objectlist>
    </response>
    </document>
    I set the list dataProvider to
    xmlDoc.response.objectlist.list.object
    Thing is, I want to remove one of the object nodes before I
    display the data (that is, i dont want one of the nodes in the XML
    document to appear in the list).
    How do I do this? I could not find any way to delete an
    existing node in the XML api.
    -Gaurav

    You can either use the "delete" operator:
    delete xmlDoc.response.objectlist.list.object[5]
    where the object to be deleted might be the fifth down
    (you'll have to play with your xml in debug to get the actual
    values down) or convert to xmlListCollection. The xmlListCollection
    provides easy methods to remove the offending nodes or you may
    provide a filterFunction for the collection which will "hide" the
    node.

  • How do I add a second email address in my email account with Shaw cable

    How do I add a second email address in my email account with Shaw cable. It is already set up with Shaw but how do I add it to my incoming email

    If you are using Apple's Mail setup is: Mail>Preferences>Accounts tab, click the + sign to add an account. You'll need the email account name and password to input on the first screen. Mail should then attempt auto-setup but if it can't do it you'll need to manually add the mail server info, etc.

  • How do i add or delete information from an existing pdf document?

    How do I add or delete information from an existing pdf document??

    The free Reader has no editing capabilities.

  • How do I add a password to a FormsCentral PDF file/document?

    How do I add a password to a FormsCentral PDF file/document?

    Hi,
    I am moving your posting at Acrobat.com to FormsCentral Forum.
    Thank you.
    Hisami

  • How do I add subtitles like what you see in a movie with black scripts both at the top and below?

    How do I add subtitles like what you see in a movie with black scripts both at the top and below?

    thank you so much that was it :). im retarded though i need to look this stuff up in the api. sorry just starting out and useally find things in the api like 75% of the time

  • How do I insert additional pages inside a footnoted document with accompanying text boxes and still keep pages below syncronized?

    Is there a way to insert additional pages into a lengthy document containing continuous footnotes at the bottom of each page and accompanying text boxes on most pages without losing the syncronization of text and text box information on the pages below?

    Footnotes do not work for text in textboxes, and you can keep on adding text after existing text so I am not sure what you are asking for.
    Peter

  • How can I convert a PDF/A file into a PDF file with Acrobat Pro X?

    How can I convert a PDF/A file into a PDF file with Acrobat Pro X? I'd like to modifiy the file which I have only as a PDF/A.

    There's two answers if you want to modify the file:
    You can temporarily turn off PDF/A Mode in your preferences, so they don't open as read-only. The problem would be if you do something to the file that violated the PDF/A standard, so you should always run a Preflight check afterwards to make sure.
    If you want to remove the PDF/A header tag, use Preflight again - it's on the Print Production Panel in Acrobat X Pro (which may be hidden, use the options menu on the Tools Pane to show it). There's a profile called "Remove PDF/A information" - choose this and press Analyze and Fix. Nothing else about the file will be altered but when you save and reopen it all the editing tools will become active.

  • How can we handle dateformat from diff countries into india in bdc with out

    hi
    experts can u help me pz
    how can we handle dateformat from diff countries into india in bdc with out chnaging system parameters while uploading.
    i.e flatfile date is germanformat
         target date is indianformat.
    thanks and regords.

    use WRITE statement to format date.. along with extension ...
    DD/MM/YY
    or
    DD/MM/YYYY

Maybe you are looking for

  • DRM-PDF with Adobe Reader on iPad

    When will it be possilbe to read DRM-PDF (encoded with LifeCycleServer) with Adobe Reader on iPad? I would like to read epapers on iPad - not on Notebook.

  • Cannot export a panel into adobe cs5 plugin/panels

    this is the error message I reecive after creating and saving the panel in my documents folder can not export the panel to the cs5 plugins\panels

  • Script: Sub totals & Grand Totals.(Urgent)

    Hi Gurus                im developing a invoice billing. for this i need hw to get sub totals and grand totals at the last page in my main window . what should be given in the text elements and in the SE 38 part.please help me with this its very urge

  • Right margin (1/2") of iPod screen unresponsive to touch-Fix?

    the right margin (about 1/2" inch) of my iPod 4th gen doesn't respond to touch. Anyone know why - anyone suggest a fix? Already tried a "restore" - it didn't work. However, for a brief while yesterday it was responsive. Now it's not again.

  • MTA shows "post office now closed/open"

    Hi, after updating agents from 8.0.2 to 8.0.3 I have MTA showing "post office now closed" and right after it "post office now open" repeated with some interval, every half an hour approx. This happens on all 5 servers (NetWare 6.5 sp8) which were upd