Avatar billede fredand Forsker
22. november 2001 - 08:40 Der er 9 kommentarer og
1 løsning

Java and XML

Hello!

Does any one know how to add a Node into a Document.
Suppose that you have a Document that looks like this
<a>
<b>1</b>
<b>2</b>
</a>
...And that you would like to add a <b>-element more?

I have loaded  a Document _document with the xml-structure above, but how do I add a new <b>?

I know how to set value like attribute or \"inner-value\", but not how to add a new element.

Best regards

Fredrik
Avatar billede martin_schou Nybegynder
22. november 2001 - 09:58 #1
if you want
<a>
<b>1</b>
<b>2</b>
<b>stuff</b>
</a>

can\'t you just read in the structure line by line, and add the line
<b>stuff</b>
when you\'re in the right position?
Avatar billede fredand Forsker
22. november 2001 - 10:06 #2
Hello!

How do you do that? Sounds really interesting.
I use org.w3c.dom for this.

So far I read a empty xml-skeleton like this:
<a>
<b></b>
<b></b>
<a>

Load it into a Document like:
FileInputStream is = new FileInputStream(_skeletonFile);
Parser parser = new Parser(_skeletonFile.getPath());
_document = parser.readStream(is);

So how do I now add a third <b> element dynamically?

I set a value into a object with:
    protected void setFieldvalue(String nodeName, String value)
    {
NodeList nodeList = _document.getElementsByTagName(nodeName);
Element node = (Element) nodeList.item(0);

NodeList children = node.getChildNodes();
children.item(0).setNodeValue(value);
}

So far the method above only work if the node name is unique.

Avatar billede martin_schou Nybegynder
22. november 2001 - 10:15 #3
If you use java.io.RandomAccessFile, you can read a line at a time with:

String currentLine = RAF.readLine();//Reads the next line of text from this file.

once you fine the right line, insert your text into the file with
RAF.writeChars(String newLine);//Writes a string to the file as a sequence of characters.

It _should_ work, but I\'m not promising anything :-).
Avatar billede fredand Forsker
22. november 2001 - 10:23 #4
Okey, but unfortunately it is to late to do the dynamic part skeleton at file-level. I need to do this with xml in the Document.
Avatar billede martin_schou Nybegynder
22. november 2001 - 10:38 #5
I take it you have the entire skeleton as a single string. If you used java.util.StringTokenizer you could split the skeleton-string into several strings with

tokenizer = java.util.StringTokenizer.StringTokenizer(String string, String deliminator)

Setting the deliminator as \"\\r\\n\" (carrige return + new line) should split the skeleton into lines.

String skeletonLine = tokenizer.nextToken()

will then return the next line in the skeleton. When you change the structure of the skeleton, you can just warp it into a single string again, and then pass it through the tokenizer again.

Or when you open the skeleton the first time, you could store it like a string-array or in a vector, with a single string per line. A linked list could work as well. It all depends on the speed at wich you want it to work.
Avatar billede lbhansen Nybegynder
25. november 2001 - 23:19 #6
You can create the element yourself by using
Element bTag = document.createElement(\"b\");
Element data = document.createCDATASection(\"someText\");
bTag.appendChild(data);

Then you can append it to the document by using
document.appendChild(bTag);
Avatar billede fredand Forsker
26. november 2001 - 15:32 #7
Hello!
Thanks for all your help, I solved it like this:
document.getElementsByTagName(nodeName).item(0).appendChild(node);

Best regards

Fredrik
Avatar billede martin_schou Nybegynder
26. november 2001 - 15:41 #8
lbhansen - jeg laver lige et spørgsmål til 80 points, så du kan få dine points:

http://www.eksperten.dk/spm/138745
Avatar billede martin_schou Nybegynder
26. november 2001 - 15:42 #9
fredand - you should have asked lbhansen to leave an answer instead of a comment, so he could have gotten the points instead of me. My answer wasn\'t used, hence I shouldn\'t get any points for it.
Avatar billede fredand Forsker
26. november 2001 - 15:45 #10
OK!
But thanks for your time any way!!
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester