2011-08-30 99 views
0

我有一個輸出,其中我想獲取CMEngine節點的值,即CMEngine節點內的所有內容。請幫我用正則表達式,我已經有了一個使用正則表達式的java代碼,所以我只需要正則表達式。由於正則表達式獲取xml節點字符串值

我的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> 
      <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> 
+0

不知道你想達到什麼,但看看這是否可以幫助:http://stackoverflow.com/questions/7008466/selecting-xml-raw-text –

+0

如果你通過任何關於SO對此,你應該看到你一般不會使用正則表達式來完成這些任務。相反,讓自己一個正確的XML解析器! –

回答

3

如果它必須是一個正則表達式,如果有每串僅一個CMEngine標籤:

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

由於該輸出似乎是機器生成和不太可能包含可能會混淆正則表達式的評論或其他內容,這應該可靠地工作。

它開始於一個位置的<CMEngine>標籤之後:(?<=<CMEngine>)
和匹配所有的字符,直到下一個</CMEngine>標籤:(?:(?!</CMEngine>).)*

+0

我測試了它[這裏](http://gskinner.com/RegExr/?2uinu),但沒有給我我想要的東西。此外,我不需要Java代碼,只需在[位置](http://gskinner.com/RegExr/?2uinu) – abi1964

+0

提供一個正則表達式@Abishek:當然,它沒有工作,你沒有設置'DOTALL'選項。 –

+0

哦,謝謝。我的錯。 「DOTALL」是什麼意思? – abi1964