Link to home
Start Free TrialLog in
Avatar of keepworking
keepworking

asked on

add a new node to an existing xml file--urgent

public static void write(String out_put_file,String s_comment,String root, String s_id, String infor)
{
//create dom document
Document doc = createDomDocument();
            
//add root
Element element = doc.createElement(root);
doc.appendChild(element);

//add comment at the beginning of the document
 Comment comment = doc.createComment(s_comment);
 element.getParentNode().insertBefore(comment, element);
            
//add nodes to dom file
doc = addNode(doc,element, s_id, infor);
            
        
//write the dom to an xml outFile
writeXmlFile(doc, out_put_file);
}

the method above is used to create a new xml file and add one node, but after xml file is created, I need add a node to this existed xml file, so I need a method like:

void add(String file_name, String comments, String root_name, String id, String infor)
how to do it in java?

thanks
ASKER CERTIFIED SOLUTION
Avatar of radarsh
radarsh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of keepworking
keepworking

ASKER

thanks radarsh:

Document doc = null;
Element element = null;
if(file_existed==false)
{
doc= createDomDocument();
                                                
element = addRoot("ROOT",doc);
addComment("testing",doc);
file_existed = true;
                                                
}
addNode(doc,element,s_id, infor);

writeXmlFile(doc, "testing.xml");

there is anything wrong, it doesn't work
static Document createDomDocument()
      {
            try
            {
                  DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
                  Document doc = builder.newDocument();
                  return doc;
            }
            catch (ParserConfigurationException e)
            {
          }
          return null;
      }

      public static Element addRoot (String s_root, Document doc)
      {
            try
            {
                  Element element = doc.createElement(s_root);
                  doc.appendChild(element);
                  return element;
            }
            catch (DOMException e)
            {
            }
            
            return null;
        
      }
      
//add comment to dom
      public static void addComment (String s_comment, Document doc)
      {
            //add comment at the beginning of the document
            Element element = doc.getDocumentElement();
          Comment comment = doc.createComment(s_comment);
          element.getParentNode().insertBefore(comment, element);
      }
      

      public static Document addNode(Document doc, Element element, String s_id, String infor)      
      {
            try
            {
                  
                  // Insert the Record element node
                Element element_Record = doc.createElement("RECORD");
                element.appendChild(element_Record);

                Element element_ID = doc.createElement("ID");
                element_Record.appendChild(element_ID);
                  
                Element element_String = doc.createElement("INFORMATION");
                element_Record.appendChild(element_String);
                  
                element_ID.appendChild(doc.createTextNode(s_id));
                element_String.appendChild(doc.createTextNode(infor));
               
            }
            catch (DOMException e){
            }
            
            return doc;
            
      }

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Mukundh:

" it will again overwrite the file given as input to the new XML. " means by your method, you will get a new xml file everytime, right?

what I need is:

there is a xml file, with one node, I need add one more node to this xml file, your method can do that?

thanks
SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ok, I finished code, maybe my question is not clear.

thanks
Which code did you use? Why the C grade?