hm5
hm5

Reputation: 65

Parse xml with empty valued attribute

Have and input of this format:

<table>
<tbody>
    <tr bgcolor='#999999'>
        <td nowrap width='1%'>
        </td>
        <td nowrap width='3%' align='center'>
            <font style='font-size: 8pt'> System ID </font>
        </td>
        <td nowrap width='5%' align='center'>   

In order to remove nowrap attribute , was earlier using this code:

    if (deletedString == null)
    {
        return exportedTable;
    }

    int tagPos = 0;
    String resultTable = exportedTable;
    while (resultTable.indexOf(deletedString) != -1)
    {
        tagPos = resultTable.indexOf(deletedString, tagPos);
        String beforTag = resultTable.substring(0, tagPos);
        String afterTag = resultTable.substring(tagPos + deletedString.length());

        resultTable = beforTag + afterTag;
    }
    return resultTable;

deletedString is nowrap, and input is exportedTable. But this is causing Performance issues. Is there any better way to do it?

Upvotes: 0

Views: 47

Answers (2)

Zim-Zam O&#39;Pootertoot
Zim-Zam O&#39;Pootertoot

Reputation: 18148

My recommendation: StringUtils.remove(source, substring) will remove all instances of the substring from the source string. This answer benchmarked this method and found it to be five times faster than a few alternatives.

Alternatively, use a StringBuilder to aggregate your substrings - every time you concatenate two strings you're creating a new string, whereas StringBuilder is mutable and doesn't need to create a new copy on an update.

Upvotes: 1

blaqksilhouette
blaqksilhouette

Reputation: 1

You could create a xmlstreamreader and have a while loop that parses through the xml as long as the streamreader.hasNext().

Format:

//Create stream reader
//Position at beginning of document

//While the stream reader has next (can see next line)
//perform action 

Upvotes: 0

Related Questions