0

I have a StringBuffer object with the following contents:

<ET>read input: 1.629ms</ET>
<ET>There were 3 errors:
<Error>
    <ErrorId>AllConditionsTrue</ErrorId>
    <MetaData>
        <Entry>
            <Key>Balance Due</Key>
            <Value>1500.99</Value>
        </Entry>
    </MetaData>
</Error>

<Error>
    <ErrorId>Opposite</ErrorId>
    <MetaData>
        <Entry>
            <Key>Node</Key>
        </Entry>
    </MetaData>
</Error>

<Error>
    <ErrorId>minInclusive</ErrorId>
    <MetaData>
        <Entry>
            <Key>Description</Key>
            <Value>Wages Amount</Value>
        </Entry>
    </MetaData>
</Error>

: 0.027ms</ET>
<ET>convert: 319.414ms</ET>
<FORM id="123"/>
<DATA size="11920"/>
<ERROR code="0"/>

How can I capture just the text which is at and within the Error Tags (<Error> some text </Error> ). So my new String or StringBuffer object contains:

<Error>
    <ErrorId>AllConditionsTrue</ErrorId>
    <MetaData>
        <Entry>
            <Key>Balance Due</Key>
            <Value>1500.99</Value>
        </Entry>
    </MetaData>
</Error>

<Error>
    <ErrorId>Opposite</ErrorId>
    <MetaData>
        <Entry>
            <Key>Node</Key>
        </Entry>
    </MetaData>
</Error>

<Error>
    <ErrorId>minInclusive</ErrorId>
    <MetaData>
        <Entry>
            <Key>Description</Key>
            <Value>Wages Amount</Value>
        </Entry>
    </MetaData>
</Error>

How can I accomplish my goal using Java?

Edit

Trying both your guys solutions:

Pattern p = Pattern.compile("<Error>.*?<\\/Error>", Pattern.DOTALL);
Matcher m = p.matcher(buf.toString());

String errorText = "";

while (m.find()) {
    errorText = m.group(1);
}

I seem to only get 3 error tag element not all 3.

Example:

<Error>
    <ErrorId>minInclusive</ErrorId>
    <MetaData>
        <Entry>
            <Key>Description</Key>
            <Value>Wages Amount</Value>
        </Entry>
    </MetaData>
</Error>
1
  • 1
    Please don't use StringBuffer as it was replaced with StringBuilder more than ten years ago. Commented Mar 15, 2016 at 7:59

3 Answers 3

1

Regex:

<Error>.*?<\/Error>

Demo

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks but this regex only seems to grab one Error tag element, see my edit to the original post
@arabian_albert check this stackoverflow.com/questions/16517689/…
Thanks @Tim007 I definitely need to readup on regex
1

SaxParse would be a better solution than string parser.

It will be portable for your future references as well.

Refer this sax documentation :

http://docs.oracle.com/javase/7/docs/api/javax/xml/parsers/SAXParser.html

Comments

0

Note that your string contains new lines, so you have to use \n. Try this out:

<Error>((?:.*?\n?)+.*?)<\/Error>

Check the Regex101

9 Comments

Trying your solution now
Thanks, your solution works for this particular problem. Now will this work for any number of Error tags?
Updated. Try it out :)
You have forgotten to double \\ at new line character \\n. You have only single one \n.
@arabian_albert while (m.find()) { System.out.println(m.group(1)); } It may print all 3. errorText = m.group(1); you will only get the last one, other is overwrite because in loop, check this stackoverflow.com/questions/6417435/…
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.