E
el_bandido
Hello,
I'm having the hardest time do the simplest things in Java. Consider
following XML document:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE KNOWHOWDB SYSTEM "null">
<KNOWHOWDB xmlns="http://www.why-is-it-so-hard.com/">
<DOCUMENT path="../aaa/bbb.ext">
<KEYWORDS>
<KEYWORD>Hello</KEYWORD>
<KEYWORD>World</KEYWORD>
</KEYWORDS>
</DOCUMENT>
<DOCUMENT path="../aaa/ccc.ext">
<KEYWORDS>
<KEYWORD>Some</KEYWORD>
<KEYWORD>gap</KEYWORD>
<KEYWORD>filler</KEYWORD>
</KEYWORDS>
</DOCUMENT>
</KNOWHOWDB>
I'm writing a Servlet that should add a new DOCUMENT and its appropriate
KEYWORDs. How can I do this? Does anyone have any good pointers? I've
been searching the net for hours now, I only get examples of replacing
an entry or creating a new XML document from scratch, but I want to add
some elements to my XML tree.
Let's say my HTML input form provided me with follwing information:
path="../bbb/aaa.ext"
keywords="some text meant as keywords"
The resulting XML tree/file would look like:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE KNOWHOWDB SYSTEM "null">
<KNOWHOWDB xmlns="http://www.why-is-it-so-hard.com/">
<DOCUMENT path="../aaa/bbb.ext">
<KEYWORDS>
<KEYWORD>Hello</KEYWORD>
<KEYWORD>World</KEYWORD>
</KEYWORDS>
</DOCUMENT>
<DOCUMENT path="../aaa/ccc.ext">
<KEYWORDS>
<KEYWORD>Some</KEYWORD>
<KEYWORD>gap</KEYWORD>
<KEYWORD>filler</KEYWORD>
</KEYWORDS>
</DOCUMENT>
<DOCUMENT path="../bbb/aaa.ext">
<KEYWORDS>
<KEYWORD>some</KEYWORD>
<KEYWORD>text</KEYWORD>
<KEYWORD>meant</KEYWORD>
<KEYWORD>as</KEYWORD>
<KEYWORD>keywords</KEYWORD>
</KEYWORDS>
</KNOWHOWDB>
My efforts so far (extremely simplified code):
----------------------------------------------
private void knowhow_add_XML() throws Exception {
DOMParser parser = new DOMParser();
parser.parse(XMLFILE);
Document doc = parser.getDocument();
Element root = doc.getDocumentElement();
NodeList descriptionElements = root.getElementsByTagName("KEYWORDS");
Element descriptionElement = (Element) descriptionElements.item(0);
Text descriptionText;
Iterator it = keywords.iterator();
while (it.hasNext()) {
descriptionElement = doc.createElement("KEYWORD");
descriptionText = doc.createTextNode(it.next().toString());
descriptionElement.appendChild(descriptionText);
root.appendChild(descriptionElement);
}
File file = new File(XMLNEW);
Writer writer = new FileWriter(file);
serializeNodeXML(doc, writer, "");
writer.flush();
writer.close();
}
The serializeNodeXML() method will write out the XML file. However I
seem to be completely unable to add any DOCUMENTS or KEYWORDs at an
intelligent place in the tree nor will the outcoming tree be correct.
I've skimmed through the SUN XML parsing documentation, through the
"Java & XML" book and "Java in a Nutshell" but I couldn't find the
answer to my simple problem. I don't want to add the append code into
the serializer as this would defeat the purpose of merging XML trees in
Java programming wise. [A beginners documentation on DOM parsing and
modifying and adding would be excellent.]
Thanks for any pointers,
Roberto Nibali, ratz
I'm having the hardest time do the simplest things in Java. Consider
following XML document:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE KNOWHOWDB SYSTEM "null">
<KNOWHOWDB xmlns="http://www.why-is-it-so-hard.com/">
<DOCUMENT path="../aaa/bbb.ext">
<KEYWORDS>
<KEYWORD>Hello</KEYWORD>
<KEYWORD>World</KEYWORD>
</KEYWORDS>
</DOCUMENT>
<DOCUMENT path="../aaa/ccc.ext">
<KEYWORDS>
<KEYWORD>Some</KEYWORD>
<KEYWORD>gap</KEYWORD>
<KEYWORD>filler</KEYWORD>
</KEYWORDS>
</DOCUMENT>
</KNOWHOWDB>
I'm writing a Servlet that should add a new DOCUMENT and its appropriate
KEYWORDs. How can I do this? Does anyone have any good pointers? I've
been searching the net for hours now, I only get examples of replacing
an entry or creating a new XML document from scratch, but I want to add
some elements to my XML tree.
Let's say my HTML input form provided me with follwing information:
path="../bbb/aaa.ext"
keywords="some text meant as keywords"
The resulting XML tree/file would look like:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE KNOWHOWDB SYSTEM "null">
<KNOWHOWDB xmlns="http://www.why-is-it-so-hard.com/">
<DOCUMENT path="../aaa/bbb.ext">
<KEYWORDS>
<KEYWORD>Hello</KEYWORD>
<KEYWORD>World</KEYWORD>
</KEYWORDS>
</DOCUMENT>
<DOCUMENT path="../aaa/ccc.ext">
<KEYWORDS>
<KEYWORD>Some</KEYWORD>
<KEYWORD>gap</KEYWORD>
<KEYWORD>filler</KEYWORD>
</KEYWORDS>
</DOCUMENT>
<DOCUMENT path="../bbb/aaa.ext">
<KEYWORDS>
<KEYWORD>some</KEYWORD>
<KEYWORD>text</KEYWORD>
<KEYWORD>meant</KEYWORD>
<KEYWORD>as</KEYWORD>
<KEYWORD>keywords</KEYWORD>
</KEYWORDS>
</KNOWHOWDB>
My efforts so far (extremely simplified code):
----------------------------------------------
private void knowhow_add_XML() throws Exception {
DOMParser parser = new DOMParser();
parser.parse(XMLFILE);
Document doc = parser.getDocument();
Element root = doc.getDocumentElement();
NodeList descriptionElements = root.getElementsByTagName("KEYWORDS");
Element descriptionElement = (Element) descriptionElements.item(0);
Text descriptionText;
Iterator it = keywords.iterator();
while (it.hasNext()) {
descriptionElement = doc.createElement("KEYWORD");
descriptionText = doc.createTextNode(it.next().toString());
descriptionElement.appendChild(descriptionText);
root.appendChild(descriptionElement);
}
File file = new File(XMLNEW);
Writer writer = new FileWriter(file);
serializeNodeXML(doc, writer, "");
writer.flush();
writer.close();
}
The serializeNodeXML() method will write out the XML file. However I
seem to be completely unable to add any DOCUMENTS or KEYWORDs at an
intelligent place in the tree nor will the outcoming tree be correct.
I've skimmed through the SUN XML parsing documentation, through the
"Java & XML" book and "Java in a Nutshell" but I couldn't find the
answer to my simple problem. I don't want to add the append code into
the serializer as this would defeat the purpose of merging XML trees in
Java programming wise. [A beginners documentation on DOM parsing and
modifying and adding would be excellent.]
Thanks for any pointers,
Roberto Nibali, ratz