473,799 Members | 3,310 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

9 New Member
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="X8UrU N79avT27LYwUESi liAV328=" name="Zahid Nawaz" phone="9599808" email="zahid.na waz@gmx.de" role="Tnr+vPuuA Asix8heVWD4mioC gLQ=" />
<user id="Admin" name="alpha beta" password="fEqNC co3Yq9h5ZUglD3C ZJT4lBs=" phone="45678265 6" email="admin@my Domain.de" role="Tnr+vPuuA Asix8heVWD4mioC gLQ=" />
<user id="Guest" name="beta alpha" password="+ml3y ZuAnbaOHFaIjsOL 0ARxmzk=" phone="8765432" email="guest@my Domain.de" role="+s6D7jAUv cj5ggPMlOLokiJF LpA=" />
<user id="Guest1" name="unknown unknown" password="+ml3y ZuAnbaOHFaIjsOL 0ARxmzk=" phone="12312211 2" email="guest1@m ydomain.de" role="+s6D7jAUv cj5ggPMlOLokiJF LpA=" /></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 2363
sezanawa
9 New Member
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
1015
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> and it could be
3
1111
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 attributes within XML. For a quick example, think of a file system represented by XML. Method 1: Attributes
4
2923
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 child element with an attribute set at a given value; maybe an example is more self-explaining... Given the following input XML document:
2
3325
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 string is in the action attribute, and if it is, remove it so the form element will look like this: <form name="form1" method="post" action="Default.aspx" id="form1"
4
2699
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 'table'" from this element "<TABLE height="689" cellSpacing="0" cellPadding="0" width="1109" border="0" ms_2d_layout="TRUE">"? How do I fix this error? -- Thanks, Mark
1
7776
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 why?/ thanks in advance
5
2912
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 Language="C#" OnLoadComplete="Page_LoadComplete" %> <%@ Import Namespace="System" %> <%@ Import Namespace="System.Web" %> <script runat="server">
5
4172
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 try and view it to Design mode i get error saying it can't be switched and then when i complile it i get these errors below !!! arg.. Attribute 'width' is considered outdated. A newer construct is recommended. Attribute 'Title' is not a valid...
4
1286
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 to retrieve an element from within that element?
0
9687
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10252
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10231
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10027
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7565
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4141
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.