www.webdeveloper.com

View Poll Results: How to create and Append nodes in XML file using JAXP1.1

Voters
0. You may not vote on this poll
  • Node nod=nl.item(0).appendChild(doc.createTextNode("G555007"));

    0 0%
  • Node nod1=nl.item(0).insertBefore(doc.createTextNode("G555007"), nl.item(1));

    0 0%
Results 1 to 4 of 4

Thread: Creating & Appending Nodes in XML

  1. #1
    Join Date
    Apr 2003
    Location
    India
    Posts
    2

    Smile Creating & Appending Nodes in XML

    Hi,

    I am using JAXP1.1 for the creation/parsing etc., of xml through Java code in one of my projects.. When I give the following codec for creating and Appending a new node for the existing xml file, it is not giving any errors in the program but, when I open the xml file for viewing the changes. The changes are not at all affected in the xml file. I think I might have missed 1 or 2 steps in b/wn, can any body tell me what I hv missied here or tell me if at all there is any other way.... I will be better if you can solve with the same. Following is my codec:

    ------
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(convertToFileURL(filename));
    NodeList nl = doc.getElementsByTagName(code);
    Node nod=nl.item(0).appendChild(doc.createTextNode("G555007"));
    Node nod1=nl.item(0).insertBefore(doc.createTextNode("G555007"), nl.item(1));
    -------

  2. #2
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    2,653
    2 things missing.

    First you have to add each node you create to the parent node.which is doc

    doc.appendChild(node)

    Second once all creation and stuff is donethen you need towrite the whole thing back to file.Otherwise all the changes are only done in memory.

    Khalid

  3. #3
    Join Date
    Apr 2003
    Location
    India
    Posts
    2

    Creating & Appending Nodes in XML

    Dear Khalid,

    Can you please tell me how can I save the document after doing the following changes;
    -------
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(convertToFileURL(filename));
    Element elem=doc.createElement("G555007");
    NodeList nl = doc.getElementsByTagName(code);
    Node nod=nl.item(0).appendChild(doc.createTextNode("G555007"));
    ------

    Actually I did as wot U told me i.e., 1st step doc.appendChild(node) but, it is giving me the following error;
    -------
    org.apache.crimson.tree.DomEx: HIERARCHY_REQUEST_ERR: This node isn't allowed there.
    org.apache.crimson.tree.DomEx: HIERARCHY_REQUEST_ERR: This node isn't allowed there.
    at org.apache.crimson.tree.XmlDocument.checkChildType(XmlDocument.java:635)
    at org.apache.crimson.tree.ParentNode.appendChild(ParentNode.java:341)
    at org.apache.crimson.tree.XmlDocument.appendChild(XmlDocument.java:683)
    at xmlFileTrans.addElem(xmlFileTrans.java:28)
    at xmlFileTrans.main(xmlFileTrans.java:9)
    -------

    But, it is not giving error if I try with what I have given above i.e.,
    -------
    Node nod=nl.item(0).appendChild(doc.createTextNode("G555007"));
    -------

    I think the only step I have missed out is Saving it back... I tried normal Java file Saving procedure for saving into the xml file... But, it has not been affected in my xml file..... Is there any other way for saving the same.... I will be thankfull to you, if you can tell me the other way for Saving the xml document after appending the child...
    Last edited by ramc352; 04-16-2003 at 04:40 AM.

  4. #4
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    2,653
    My bad,I did not read carefully what code you had.Ok here is what you are trying to do.

    1. create document object from an xml file.
    2. get a particular node
    3. append some text to that node.
    4. now you want to save it on file.

    append to document thing would be required if you were creating the hole document from scratch.

    What you are doing above, in that why are you creating an element??? since you are not using it.to make that element appear in your xml it has to beappended to either root element or any other node.
    Below is the code snipped that you will need to print it.

    Code:
    try {
    
          TransformerFactory tFactory = TransformerFactory.newInstance();
          Transformer transformer = tFactory.newTransformer();
           /*
          //where dtdtarget is equal to a valid dtd resource e.g "http://presslogic.com/dtd/DTDAddressAttributeGroup"
          if(dtdtarget!=null){
            transformer.setOutputProperty(
                javax.xml.transform.OutputKeys.DOCTYPE_PUBLIC,
                "-//W3C//Presslogic DTD Repository//EN"
              );
            transformer.setOutputProperty(
                javax.xml.transform.OutputKeys.DOCTYPE_SYSTEM,dtdtarget
              );
          }*/
          DOMSource source = new DOMSource(doc);
          StreamResult result = new StreamResult(out);
          transformer.transform(source, result);
        } catch (TransformerConfigurationException tce) {
          tce.printStackTrace(System.err);
        } catch (TransformerException te) {
          te.printStackT
    you may have to import transformation classes from
    javax.xml.transform.....

    Hope this helps

    Cheers

    Khalid

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles