22. november 2001 - 08:40Der 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.
I dette særtema ser vi på, hvordan cloud og AI bliver fundamentet for virksomhedernes digitale forretning, og hvordan de nye muligheder for automatisering og forretningsværdi kan udnyttes uden at miste overblik, sikkerhed og menneskelig kontrol.
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.
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 :-).
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
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.
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);
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.
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.