开发者

Regex to fetch xml node string value

I have an output, where i'd like to fetch the value of CMEngine node i.e., everything inside CMEngine node. Please help me with a regex, I already have a java code in place which uses the regex, so I just need the regex. Thanks

My XML

<General>
    <LanguageID>en_US</LanguageID>
<CMEngine>
    <CMServer/> <!-- starting here -->
    <DaysToKeepHistory>4</DaysToKeepHistory>
    <PreprocessorMaxBuf>5000000</PreprocessorMaxBuf>
    <ServiceRefreshInterval>30</ServiceRefreshInterval>
    <ReuseMemoryBetweenRequests>true</ReuseMemoryBetweenRequests>
    <Trace Enabled="false">
        <ActiveCategories>
            <Category>ENVIRONMENT</Category>
            <Category>EXEC</Category>
            <Category>EXTERNALS</Category>
            <Category>FILESYSTEM</Category>
            <Category>INPUT_DOC</Category>
            <Category>INTERFACES</Category>
            <Category>NETWORKING</Category>
  开发者_如何学JAVA          <Category>OUTPUT_DOC</Category>
            <Category>PREPROCESSOR_INPUT</Category>
            <Category>REQUEST</Category>
            <Category>SYSTEMRESOURCES</Category>
            <Category>VIEWIO</Category>
        </ActiveCategories>
        <SeverityLevel>ERROR</SeverityLevel>
        <MessageInfo>
            <ProcessAndThreadIds>true</ProcessAndThreadIds>
            <TimeStamp>true</TimeStamp>
        </MessageInfo>
        <TraceFile>
            <FileName>CMEngine_log.txt</FileName>
            <MaxFileSize>1000000</MaxFileSize>
            <RecyclingMethod>Restart</RecyclingMethod>
        </TraceFile>
    </Trace>
    <JVMLocation>C:\Informatica\9.1.0\java\jre\bin\server</JVMLocation>
    <JVMInitParamList/>  <!-- Ending here -->
</CMEngine>
</General>


If it has to be a regex, and if there is only one CMEngine tag per string:

Pattern regex = Pattern.compile("(?<=<CMEngine>)(?:(?!</CMEngine>).)*", Pattern.DOTALL);
Matcher regexMatcher = regex.matcher(subjectString);
if (regexMatcher.find()) {
    ResultString = regexMatcher.group();
}

Since that output appears to be machine-generated and is unlikely to contain comments or other stuff that might confuse the regex, this should work quite reliably.

It starts at a position right after a <CMEngine> tag: (?<=<CMEngine>)
and matches all characters until the next </CMEngine> tag: (?:(?!</CMEngine>).)*.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜