In this document if i want to insert another element using Xpath, say,<publisher> in <book> node, how to insert it. I'm using DOM parser for parsing the xml file. Any help to solve this issue appreciated.
Thanks for your reply.. But how can i find the exact path to insert the element. There are two nodes <Person> occur in the xml document. If i want to insert it in the second <person> node then how to find the exact path of it? I'm trying this for last two days so i'm waiting for your help. Thanks in advance.
I'll get get the node name and its value and the position to add the node all in run time only. I'm displaying the xml in a form display in a jsp page and i have a check box for every node. So when i click a node i'll display the elements that can be added under that corresponding node using the DTD of that xml. Hereby i'm getting the node name its textvalue and Xpath.. I couln't get the xpath correctly and so i couln't add that element in the proper node. Here is my code.... It adds the node name and node value but not in the correct location...It adds under the first <person> even if select second <person>
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
Document document = documentBuilderFactory.newDocumentBuilder().parse(fname);
XPath xpath = XPathFactory.newInstance().newXPath();
//getting the node name where child node has to be added and adding the node
Node node = (Node) xpath.evaluate(nodepath, document, XPathConstants.NODE);
Source source = new DOMSource(document);
File file = new File(fname);
Result result = new StreamResult("books.xml");
Transformer transformer = TransformerFactory.newInstance().newTransformer();
i'm going wrong in the nodepath.. Can u give me any idea of how to solve this issue?
Yes you are correct but i dont know to get the Xpath with the index (Person). i manipulate only with
Still now i proceeded the xpath without the index as i couldn't get the index. I tried in my code where i parsed the document to find the xpath and didnt find the solution since i want my code to be generic for all xml files.Do you have any idea of how to find the xpath with its index. Your help is expected.
Thanks.. your code works perfect if i know the position of insertion before itself. But in my project the user may select any position that is they may choose any <person> node for inserting the child nodes. So finding the xpath with index position may provide a solution.
What i'm trying to do is an xml editor that allows editing of any xml file. Can you get me my idea now?
But in my project the user may select any position that is they may choose any <person> node for inserting the child nodes.
Ok so now I get it..you want the index to be user-chosen. Now if the list of users is a drop-down,I think you can use a scripting language like jquery to hold the value chosen and pass it as a variable in your xpath. Sounds far-fetched,but you can give it a try...
U got it correctly.. The user will choose the node by clicking a check box under which they have to insert the child node. Then i'll read the DTD and display the user that what are the child nodes that are allowed to add under that node. After that i'll pass the xpath of the parent node and the chilld node to a servlet where i'll perform the node insertion into the correponding xml doument. If i fetch the xpath along with its index then i can insert it correctly. I'm working on it. I'll post once the issue get resolved.