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>
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
arabian_albert
  • 708
  • 1
  • 11
  • 24

3 Answers3

1

Regex:

<Error>.*?<\/Error>

Demo

Tim007
  • 2,557
  • 1
  • 11
  • 20
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

Parth Joshi
  • 452
  • 4
  • 6
0

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

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

Check the Regex101

Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183