473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to remove an Element from XML by confirming Attribute of that element

Hi guys

I have an XML file where i have all users DB. Now i want to remove a user from that XML file. I want to check an user id attribute which is uniqe with existing users in XML file, if its the same user then delete this user from XML file and save the changes in XML file.

here is my XML file:
[HTML]
<?xml version="1.0" encoding="UTF-8"?>
<users>
<user id="zahid" password="X8UrUN79avT27LYwUESiliAV328=" name="Zahid Nawaz" phone="9599808" email="zahid.nawaz@gmx.de" role="Tnr+vPuuAAsix8heVWD4mioCgLQ=" />
<user id="Admin" name="alpha beta" password="fEqNCco3Yq9h5ZUglD3CZJT4lBs=" phone="456782656" email="admin@myDomain.de" role="Tnr+vPuuAAsix8heVWD4mioCgLQ=" />
<user id="Guest" name="beta alpha" password="+ml3yZuAnbaOHFaIjsOL0ARxmzk=" phone="8765432" email="guest@myDomain.de" role="+s6D7jAUvcj5ggPMlOLokiJFLpA=" />
<user id="Guest1" name="unknown unknown" password="+ml3yZuAnbaOHFaIjsOL0ARxmzk=" phone="123122112" email="guest1@mydomain.de" role="+s6D7jAUvcj5ggPMlOLokiJFLpA=" /></users>

[/HTML]


for example i want to delete a user which have user id= Guest.

How can i do it in Java. Any code example please. i am using JDOM document and SAXBuilder for parsing.

Waiting for your replay.

Thanks in Advance

Best regard
Nov 14 '07 #1
1 2343
Hi,

Well after waiting a week where nobody has written back me. I started digging into XML, Jdom library. There i find very simple solution for deleting an element from XML file by confirming its attribute value.here i am posting my solution.
Expand|Select|Wrap|Line Numbers
  1. public boolean removeUserByID(String id){
  2.         String fileName = "C:/users.xml";
  3.         SAXBuilder builder = new SAXBuilder();
  4.         Document doc = builder.build(fileName );
  5.         Element root = doc.getRootElement();
  6.         Iterator userListe = root.getChildren("user").iterator();
  7.         while (userListe.hasNext()) {
  8.             Element eUser = (Element) userListe.next();
  9.             if(eUser.getAttribute("id").getValue().equals(id)){
  10.  
  11.                 root.removeContent(eUser);
  12.                 saveInFile(fileName, doc);
  13.                 return true;
  14.             }
  15.         }
  16.         return false;
  17.     }
  18.  
  19.  
  20.  public String saveList(String fileName, Document doc) {
  21.         try {
  22.             FileOutputStream fos = new FileOutputStream(fileName);
  23.             XMLOutputter out = new XMLOutputter();
  24.             out.output(doc, fos);
  25.             return "true";
  26.  
  27.         } catch (Exception e) {
  28.             return "false";
  29.         }
  30.     }
  31.  
So its the whole code to delete and save back in xml file

bye
Nov 23 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: davidw | last post by:
I need store some setting in XML. I can not decide to use xml element or xml attribute to store them, for example, the XML could be <Page> <Label>test</Label> <Size>10</Size> ..... </Page> ...
3
by: pagates | last post by:
Hi All, This is a pretty simple question, but with possibly a complex answer. I was wondering if anybody had any opinions or links for "rules" as to when to use elements and when to use...
4
by: patrizio.trinchini | last post by:
Hi all, I'm new to XSLT and maybe my problem have a very trivial answer, but I need an expert that point me in the right direction. What I would obtain is to remove all the elements that have a...
2
by: eggie5 | last post by:
This is a snippet from my page: <form name="form1" method="post" action="Default.aspx?id=365272" id="form1" enctype="multipart/form-data"> I need some javascript that can check if the id query...
4
by: Mark | last post by:
Hi, I am trying to port an application for ASP.NET 1.1 to ASP.NET 2.0. Why am I getting "Error 1 Validation (Internet Explorer 6): Attribute 'ms_2d_layout' is not a valid attribute of element...
1
by: perspolis | last post by:
Hi all I created a Web Project but it dosen't support some properties of HTML tags and it gives me following error: Attribute 'height' is not a valid attribute of element 'table' I don't know...
5
by: =?Utf-8?B?bXBhaW5l?= | last post by:
So this one is confusing as everything works but I want this warning to go away. This is what I have (within a .NET 2.0 Web Application, using VS.NET 2008 SP1): default.aspx: <%@ Page...
5
by: rote | last post by:
I have a webpart project i copied across and after setting up in VS 2005 all good. When i go to the design mode and try and autofornat my webpartzone to for example Simple or Professional. The i...
4
Frinavale
by: Frinavale | last post by:
Is there a method like document.getElementByID() which will retrieve an element from another DOM element? Rephrasing: Say I retrieve a Div element using the getElementID method, is there a method...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.