123456789012</number></abc>\"" />
开发者

Java Regex - exclude empty tags from xml

let's say I have two xml strings:

String logToSearch = "<abc><number>123456789012</number></abc>"

String logToSearch2 = "<abc><number xsi:type=\"soapenc:string\" /></abc>"

String logToSearch3 = "<abc><number /></abc>";

I need a pattern which finds the number tag if the tag contains value, i.e. the match should be found only in the logToSearch.

I'm not saying i'm looking for the number itself, but rather that the matcher.find method should return true only for the first string.

For now i have this: Pattern pattern = Pattern.compile("<(" + pattrenString + ").*?>", Pattern.CASE_INSENSITIVE); where the patternString is simply "number". I tried to add "<(" + pattrenString + ")[^/>].*?> but it didn't开发者_Go百科 work because in [^/>] each character is treated separately.

Thanks


This is absolutely the wrong way to parse XML. In fact, if you need more than just the basic example given here, there's provably no way to solve the more complex cases with regex.

Use an easy XML parser, like XOM. Now, using xpath, query for the elements and filter those without data. I can only imagine that this question is a precursor to future headaches unless you modify your approach right now.


So a search for "<number[^/>]*>" would find the opening tag. If you want to be sure it isn't empty, try "<number[^/>]*>[^<]" or "<number[^/>]*>[0-9]"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜