Deleting records from XML file--just a little problem

Hi!

My xml file has a simple form like this:

<?xml version="1.0" encoding="UTF-8"?>

<AdressBook>

<Name>Suzy</Name>

<Lastname>Love</Lastname>

<Adress>You street 22</Adress>

<Phone>911</Phone>

<Email>loveme@gmail.com</Email>

</Record>

<Record>

<Name>Judy</Name>

<Lastname>Goblin</Lastname>

<Adress>Milkyway</Adress>

<Phone>911</Phone>

<Email>goblin_judy@gmail.com</Email>

</Record>

...

...

</AdressBook>

Now, whenever I delete a record from that file it leaves a remaining tag <Record /> on the loction were previous record was. How do I delete that remaining tag or better yet, how do I delete Record element in whole. I am using jdom.

Here is the code I use to delete records:

publicvoid deleteRecordFromFile(Record r)

{

Element root=doc.getRootElement();

List records=root.getChildren();

Iterator listIt=records.iterator();

while(listIt.hasNext())

{

Element rec=(Element)listIt.next();

Record p=new Record("","","","","");

List recChildren=rec.getChildren();

Element e=null;

for(int i=0;i<recChildren.size();i++)

{

e=(Element)recChildren.get(i);

switch (i)

{

case 0: p.setName(e.getText());break;

case 1: p.setLastName(e.getText());break;

case 2: p.setAddress(e.getText());break;

case 3: p.setPhone(e.getText());break;

case 4: p.setEmail(e.getText());break;

default: ;

}

}

if(p.equals(r))

{

rec.removeContent();

System.out.println("Record deleted!");

}

}

writeToFile(filename);

}

Like you see above I use the method removeContent(), should I use some other method? I need to get rid of that remaining tags in order to make my editing of the records stored in there easier. I guess I could make a seperate routine that would clean my file of those tags, but I think that is just too much time consuming...or not?

Please help me out here.:)

Message was edited by:

byteminister>

[3423 byte] By [byteministera] at [2007-11-26 22:37:13]
# 1

Does the "rec" variable contain a reference to a "Record" element? I am guessing it does. Now I don't use JDOM but I think the removeContent() method removes all the children (content == children) from an element. You could check the documentation, but I think that is what it does because that is what your program actually did.

If you want to remove the "Record" element then you need a method that removes "rec" from the document, or from its parent, or something like that. My intuition is that the method would be named "remove", but it could be something else. Check the documentation.

DrClapa at 2007-7-10 11:48:06 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

You cannot remove the elements of a List while iterating over them without the risk of throwing a ConcurrentModificationException, whatever method you would use. So you can only delete the records in two steps: collect them all, and delete them afterwards.

public void deleteRecordFromFile(Record r)

{

Element root=doc.getRootElement();

List records=root.getChildren();

Iterator listIt=records.iterator();

// Create a container for storing references to the records that will be deleted.

ArrayList<Element> deleteList = new ArrayList<Element>();

while(listIt.hasNext())

{

Element rec=(Element)listIt.next();

Record p=new Record("","","","","");

List recChildren=rec.getChildren();

Element e=null;

for( int i=0;i<recChildren.size();i++)

{

e=(Element)recChildren.get(i);

switch (i)

{

case 0: p.setName(e.getText()); break;

case 1: p.setLastName(e.getText());break;

case 2: p.setAddress(e.getText());break;

case 3: p.setPhone(e.getText());break;

case 4: p.setEmail(e.getText());break;

default: ;

}

}

if(p.equals(r))

{

// This record will be deleted.

deleteList.add(rec);

}

}

// Delete all the records in deleteList.

records.removeAll(deleteList);

writeToFile(filename);

}

>

prgguya at 2007-7-10 11:48:06 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3
prgguy, thanks for the solution:D
byteministera at 2007-7-10 11:48:06 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...