2013-08-20 26 views
1

正則表達式我有這個字符串中的XML格式問題在Java

<?xml version="1.0" encoding="UTF-8"?>n 
    <objectA>n 
    <command>Existing</command>n 
    <status>OK</status>n 
    <values>a lot of values....</values>n 
    </objectA> 

我想它只是用價值和內容

 [0] objectA = "" 
    [1] command = Existing 
    [2] status = ok 
    [3] values = a lot of values.... 

我試圖

拆分爲字符串數組
result = result.replaceAll(">n<", "><"); //$NON-NLS-1$ //$NON-NLS-2$ 
Pattern pattern = Pattern.compile("<*?>*?</*?>"); //$NON-NLS-1$ 

但是對我不起作用

+0

我不確定,但您是否嘗試過先替換所有換行符(\ n),然後運行您的模式。什麼不起作用?任何錯誤消息? – ZeusNet

+0

爲什麼你不使用xml解析器? – mishadoff

+0

您最好使用XML解析器 –

回答

1

爲什麼不使用XML解析器?

Element docElem = document.getDocumentElement(); 

NodeList children = docElem.getChildNodes(); 

List<String> values = new ArrayList<String>(); 

for(int x = 0; x < children.getLength(); x++) 
{ 
    // Do what you want with children. That came out wrong. 
}