TestingInProd
TestingInProd

Reputation: 379

Java - Remove consecutive duplicate tags in a string java

I have a string which has a xml in it.I would like to remove all consecutive duplicate tags in it using java. Here is the output i am getting

<?xml version="1.0" encoding="UTF-8"?>
<Student>
    <Student>
        <Name>Clark Kent</Name>
        <ID>555-12-3456</ID>
        <AssignedWork>
            <AssignedWork>
                <category>Exams</category>
                <GradedWork>
                    <GradedWork>
                        <Name>Mid Term</Name>
                        <Grade>20</Grade>
                    </GradedWork>
                    <GradedWork>
                        <Name>Final</Name>
                        <Grade>80</Grade>
                    </GradedWork>
                </GradedWork>
            </AssignedWork>
        <AssignedWork>
    </Student>
</Student>

I would like to remove one instance of <Student>,</Student>,and <AssignedWord> but not <Name> because it is not cosecutive. How do i do it in java?

I tried the below , but it will delete <Name> also.

String opstring = new LinkedHashSet<String>(Arrays.asList(xmlString.split(">"))).toString().replaceAll("(^\\[|\\]$)", "").replace(", ", ">");

Upvotes: 1

Views: 280

Answers (2)

KP_JavaDev
KP_JavaDev

Reputation: 222

Here is an approach. I think you can delete duplicate repeated string. Pass all the strings in String [] array. Take all the tag for ex : < name > as token in String array and then you can pass all token in HashMap as a key and then filter it with your requirement and delete it. Please try once.

Upvotes: 0

R.G
R.G

Reputation: 7131

Your example does not have an tag as given in your explanation and I assume the second tag is not closed by mistake.

Following code uses LinkedList . Not sure if you are looking for a one-liner.

String[] elements = xml.split("<");
LinkedList<String> ll = new LinkedList<String>();
for (String str : elements) {
    if (str.isEmpty())
        continue;
    str = str.trim();
    if (ll.isEmpty()) {
        ll.add(str);
        continue;
    }
    if (!ll.peekLast().equals(str)) {
        ll.add(str);
    }
}
while (!ll.isEmpty()) {
    System.out.println("<" + ll.pollFirst());
}

Upvotes: 1

Related Questions