Java 是否从原始文本文件获取所有XML?

Java 是否从原始文本文件获取所有XML?,java,xml,regex,parsing,Java,Xml,Regex,Parsing,我有一个日志文件,我需要编写程序,从这个文件中获取所有xml。 文件看起来像 text text xml text xml text etc 您能给我一些建议吗?使用regexp或其他什么更好? 也许可以用dom4j来做这件事? 如果我尝试使用regexp,我会看到下一个问题,即文本部分有标记 更新1: XML示例 SOAP message: <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envel

我有一个日志文件,我需要编写程序,从这个文件中获取所有xml。 文件看起来像

text
text
xml
text
xml
text 
etc
您能给我一些建议吗?使用regexp或其他什么更好? 也许可以用dom4j来做这件事?
如果我尝试使用regexp,我会看到下一个问题,即文本部分有
标记

更新1: XML示例

  SOAP message:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
 here is body part of valid xml
</soapenv:Body>
</soapenv:Envelope>
text,text,text,text
symbols etc
  SOAP message:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
 here is body part of valid xml
</soapenv:Body>
</soapenv:Envelope>
text,text,text,text
symbols etc
SOAP消息:
以下是有效xml的主体部分
文本,文本,文本,文本
符号等
SOAP消息:
以下是有效xml的主体部分
文本,文本,文本,文本
符号等

谢谢。

如果您的XMl总是在一行上,那么您可以在多行上迭代检查它是否以
开头。如果每个这样的部分都在单独的行中,那么它应该非常简单:

s = s.replaceAll("(?m)^\\s*[^<].*\\n?", "");

s=s.replaceAll((?m)^\\s*[^逻辑是获取所有XML,带标签,并忘记文件中的其他文本是的,我明白了,但如果XML不在单独的行上呢?正如我在回答中所说的,如果……那么这将起作用,否则……用一些示例更新您的问题,这样我就可以处理一些问题,我将尝试为您找到正确的解决方案……@IshikawaYoshi-您应该使用then解析器,而不是这类任务的t正则表达式…^[A-Za-z][A-Za-z]{0,4}+[\n]*$
Discarding line: `hello`
Discarding line: `this is some text`
Discarding line: `foo is bar`
Problem parsing line: `<this is not valid XML` as XML

Found 3 XML documents.
<foo>I am XML</foo>
<bar>me too!</bar>
<foo><bar>so am I</bar></foo>
s = s.replaceAll("(?m)^\\s*[^<].*\\n?", "");