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_commen t);
element.getParentNode().in sertBefore (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
{
//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_commen
element.getParentNode().in
//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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
static Document createDomDocument()
{
try
{
DocumentBuilder builder = DocumentBuilderFactory.new Instance() .newDocume ntBuilder( );
Document doc = builder.newDocument();
return doc;
}
catch (ParserConfigurationExcept ion 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_commen t);
element.getParentNode().in sertBefore (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(elemen t_Record);
Element element_ID = doc.createElement("ID");
element_Record.appendChild (element_I D);
Element element_String = doc.createElement("INFORMA TION");
element_Record.appendChild (element_S tring);
element_ID.appendChild(doc .createTex tNode(s_id ));
element_String.appendChild (doc.creat eTextNode( infor));
}
catch (DOMException e){
}
return doc;
}
{
try
{
DocumentBuilder builder = DocumentBuilderFactory.new
Document doc = builder.newDocument();
return doc;
}
catch (ParserConfigurationExcept
{
}
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_commen
element.getParentNode().in
}
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(elemen
Element element_ID = doc.createElement("ID");
element_Record.appendChild
Element element_String = doc.createElement("INFORMA
element_Record.appendChild
element_ID.appendChild(doc
element_String.appendChild
}
catch (DOMException e){
}
return doc;
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
" 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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok, I finished code, maybe my question is not clear.
thanks
thanks
Which code did you use? Why the C grade?
ASKER
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