• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

How to remove element in xml ?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the xml i am working on :

<stan:standardHeader>
<stan:serviceState>
<stan:stateCode/>
</stan:serviceState>
<stan:serviceAddressing>
<stan:from>http://www.profile.com?BTCOM</stan:from>;
</stan:serviceAddressing>
</stan:standardHeader>
<manageClientProfileReq>
<man:clientProfile>
<get:client>
<user:id/>
<ccm:action>delete</ccm:action>
<user:clientIdentifier>
<user:value/>
</user:clientIdentifier>
<user:clientIdentity>
<ccm:updateKey/>
<ccm:action>vikash</ccm:action>
<user:managedIdentifierDomain>
<res:value>FVC</res:value>
</user:managedIdentifierDomain>
</user:clientIdentity>
</get:client>
</man:clientProfile>
</manageClientProfileReq>


I have to remove the value in the tag <ccm:action></ccm:action> ; the one that is in the <get:client> tag .

Here Is the code i am using :

NodeList parentNodeList = document.getElementsByTagName("get:client");

for (int i = 0; i < parentNodeList.getLength(); i++)
{

Node parentNode = parentNodeList.item(i);
Element parentElement = (Element)parentNode ;


if(parentNode.getNodeType() == Node.ELEMENT_NODE)
{
NodeList childNodeList = parentElement.getElementsByTagName("ccm:action");
for(int j=0 ; j < childNodeList.getLength() ; j++)
{
Node childNode = childNodeList.item(0);


parentElement.removeChild(childNode);

Element newNode = document.createElement("ccm:action");

parentElement.appendChild(newNode);


System.out.println(" A new node was created ");
}
}

This shows an error :
NOT_FOUND_ERR: An attempt is made to reference a node in a context where it does not exist.


How can i remove value in that tag without this error ?

This approach was used since i could not simply remove the value in the tag however thats the actual requirement . This one works but for that the new node replacing the earlier one cant have the same name . Please help .
 
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Use Code tags for better readibility. BTW Welcome to Javaranch.

In the for lopp what is the use of 'j' inside it. childNodeList.item(0) will always point to the first tag .
Also parentElement.getElementsByTagName("ccm:action"); will always fetch the first tag as in Document order. So you got to use above for loop to delete all the <ccm:action> first and then create a empty one by refering the parent element.

Also remove,updation is available only in DOM object , not in the XML , it has to be written back to XML to store it.

 
aakash agarwal
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sadhasivam ,
Thanks for the reply .
But my requirement states that only the data in the first <ccm:action> tag has to be removed and not all the <cc:action> tags .
This code will remove all of them and that is going to create problems for me .
Kindly help me with how to remove only the data in the tag .


I am using the following code to create a new xml file containing the modified dom tree modified using following code . However the changes don't reflect in this file ( new file named file2 ) and i don't get any error too . The changes do show up when the same file as input is specified in the result object . Please respond asap .




 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aakash,


Kindly help me with how to remove only the data in the tag .



Check setNodeValue() of Element Interface. If you need only the first tag to set to blank , use item(0) for each search and update the setNodeValue() to blank.




Also the code that you shared to write back to file is what you tried ? you got to write the entire 'document ' and not the 'node' alone. check some tutorial to how to write
 
aakash agarwal
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Thanks for the response.
The above code also doesnt show any changes in the new file created. In the tutorial also the same code is written, What can be the mistake here ? Please reply.

And here is the use of the setNodeValue() to the child element. This compiles and runs without any effect on the xml and no errors too.

 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How are the two codes related to each other. You have to use the same 'document' object that you created to parse the XML to write it out. As in the code that you have provided , you created a new document object and writing to a different file. please show up the complete code and check if you write things in sequence
 
aakash agarwal
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With sincere apologies for all the messing around and confusions i am copying the entire code.
My requirement is :
To remove the text in the node and write the entire tree to a new file. Since i could not change the value in the node i deleted the node and tried creating a new one with the same name.

This code works completely fine with the following conclusions :

1. A new file gets created with no changes if I specify "file2" in the StreamResult result = new StreamResult(file) .
2. With "file" in the StreamResult result = new StreamResult(file), changes are reflected accordingly but only when the new tag created is named other then " < ccm:action> .

 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aakash,


First thing remove the for loop which runs unnecessarily , though you need just item(0) . check out if results are as expected

Second try replace Child method instead of remove and add. when you create a element specify the value as blank.
 
aakash agarwal
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
org.w3c.dom.DOMException: NOT_FOUND_ERR: An attempt is made to reference a node in a context where it does not exist.
at org.apache.xerces.dom.ParentNode.internalInsertBefore(Unknown Source)
at org.apache.xerces.dom.ParentNode.replaceChild(Unknown Source)
at example.removeElementData(example.java:298)Row No.: 10

This is the error on using the modifications suggested by you. Here is the modified code. Now I am getting over the whole thing.


Also please let me know why is the changes being made not reflected to the new file.
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Aakash,

replaceChild takes newNode as first argument and old as second argument , you have replaced , hence the NOT_FOUND ERR. You can use the same tagName to replace. Only difference i see , you are using Namespaces. (ccm , user ..).


Also please let me know why is the changes being made not reflected to the new file.



Only if your program runs completely, you get the new Modified file. You can specify different file. It should work

 
Arch enemy? I mean, I don't like you, but I don't think you qualify as "arch enemy". Here, try this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic