1

I need to retain the child nodes by deleting its parent node..
is it possible?

Example:

<?xml version="1.0" encoding="UTF-8"?>
<School>
  <SSLC>
    <name></name>
    <rollno></rollno>
  </SSLC>
  <PUC>
    <first_pu>
      <name></name>
      <rollno></rollno>
    </first_pu>
    <second_pu>
      <name></name>
      <rollno></rollno>
    </second_pu>
  </PUC>
</School>

I need to delete <PUC> node. I have tried the following code:

String lineToRemove = "<PUC>";
String currentLine;

while ((currentLine = reader.readLine()) != null)
{
    String trimmedLine = currentLine.trim();
    if (trimmedLine.equals(lineToRemove)) continue;
    writer.write(currentLine + System.getProperty("line.separator"));
}

But it's not working.

Mike Laren
  • 8,028
  • 17
  • 51
  • 70
user3497375
  • 45
  • 2
  • 2
  • 6
  • and should delete parent node.. – user3497375 Apr 09 '15 at 14:51
  • you can append all xml lines in StringBuffer and then remove all occurences of in that – user2408578 Apr 09 '15 at 14:53
  • I didn´t test it, but I think in the same code you have, you could do ´trimmedLine = trimmedLine.replaceAll(lineToRemove, "").replaceAll("", "")´. Then write ´trimmedLine´, maybe you could check if it's not empty...Is not very elegant, but is a quick fix to what you have. – maxivis Apr 09 '15 at 14:57
  • @maxivis Thanks.. I gave it as `tring trimmedLine = currentLine.trim(); trimmedLine.replaceAll(lineToRemove, "").replaceAll("", ""); if(trimmedLine.equals(lineToRemove)) continue; writer.write(currentLine + System.getProperty("line.separator"));` But its not working :( – user3497375 Apr 09 '15 at 15:06
  • 1
    [Why are you not using an XML parser?](http://stackoverflow.com/a/1732454/1079354) – Makoto Apr 09 '15 at 16:03
  • On a very serious note, what is it you're attempting to accomplish? What do you want to happen to the children nodes? What's an example of this code "working"? (Saying that it doesn't work is *not* the start of a good, answerable question.) – Makoto Apr 09 '15 at 16:04
  • Hi @Makoto, I assume that you downvoted both answers, I would like to know why, considering that we just provided a very quick fix to the problem posted here. As I said before, I know there's a lot of ways to do it better, but we try to help taking into account the initial code of the user. Regards – maxivis Apr 09 '15 at 16:21
  • I'm not sure why you'd assume *both*, but...I'm looking at this particular problem and any time someone elects to use regex for dealing with XML, serious questions are raised. It may be an [XY Problem](http://meta.stackexchange.com/a/66378/175248), or it could be my own personal PTSD from using regex in an XML document. In either event, I feel like more information from the OP is in order on this one, as knowing their actual objective would go a long way to giving a good answer as opposed to one that addresses what seems to be a symptom of the real issue. – Makoto Apr 09 '15 at 16:24

1 Answers1

-1

I think you misunderstood my suggestion. replaceAll(regex,replacement) will remove (in this case) from your line all the occurrences of "<PUC>" and "</PUC>" so it has no sense to check if(trimmedLine.equals(lineToRemove)) it will be always false. Based on your code:

String lineToRemove = "<PUC>";
String currentLine;

while ((currentLine = reader.readLine()) != null)
{
    String trimmedLine = currentLine.trim().replaceAll("<PUC>", "").replaceAll("</PUC>", "");
    if(trimmedLine.isEmpty()) continue;
    writer.write(currentLine + System.getProperty("line.separator"));
}

Again, this is not elegant at all, there are thousands ways to do it better, but I'm at work so I'm a little busy and can't take too much time :P. Hope this helps, regards.

Itai Bar-Haim
  • 1,686
  • 16
  • 40
maxivis
  • 1,727
  • 3
  • 21
  • 35