2014-09-12 31 views
0

我有一個我的java字符串一個XML字符串如下對象:得到一個字符串的元素,它看起來像XML和操縱它在Java

<Record><op>Add</op><sensdata>400188711111</sensdata><id>4</id><a1>1111201090467034</a1></Record> 

我需要得到sensdata標籤之間的數據掩蓋它像4001887XXXXX,並準備下面的XML字符串並記錄下來。

<Record><op>Add</op><sensdata>4001887XXXXX</sensdata><id>4</id><a1>1111201090467034</a1></Record> 

sensdata標記可以位於較低或較高位置。

有什麼更好的方法來做到這一點?我是否必須使用一些字符串操作或正則表達式或XML解析器來執行此操作?

我有一個附加到這個問題的小查詢。如果我需要

之間

<Record> </Record> 

<op>Add</op><sensdata>4001887XXXXX</sensdata><id>4</id><a1>1111201090467034</a1> 
數據我可以得到使用XML解析器。我能夠得到像Add4001887XXXXX41111201090467034這樣的值。但沒有標籤。

+12

它的XML - 所以使用XML解析器。 – 2014-09-12 13:33:37

+0

使用Xpath表達式來獲取值,然後使用DOM庫來附加到XML。 – Sid 2014-09-12 13:35:12

+0

只是請[不要使用正則表達式。](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – 2014-09-12 13:35:36

回答

1

當你的字符串包含的XML,你應該使用XML解析器。一個適當的例子可以發現here。最重要的是你的問題得到妥善的解決辦法是:

ByteArrayInputStream stream = new ByteArrayInputStream("<Record><op>Add</op><sensdata>400188711111</sensdata><id>4</id><a1>1111201090467034</a1></Record>".getBytes()); 
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
Document document = builder.parse(stream); 

NodeList sensdata = document.getDocumentElement().getElementsByTagName("sensdata"); 

現在你有sensdata節點列表。你可以進一步操縱它。要使用給定節點的字符串值,你可以進行如下操作:

String sensData = sensdata.item(0).getTextContent(); 

也許這將是最好寫的防守代碼在這種情況下,爲了避免NPE因此,上述方案應加以防護:

if (sensdata.getLength() > 0) { 
    String sensData = sensdata.item(0).getTextContent();   
} 

由於還指出,在其他的答案人們也可以XPath的使用:

XPath xPath = XPathFactory.newInstance().newXPath(); 
String data = xPath.compile("/Record/sensdata").evaluate(document); 
+0

非常感謝大家的幫助。我用XML解析,它爲我工作。 – Anita 2014-09-12 14:28:21

+0

@Anita很高興有幫助! – 2014-09-12 14:39:34

0

這是如果你不想使用任何XML解析器。

在嘗試查找sensdata之前,您可以使用toLowerCase()。不要使用原始字符串來保留區分大小寫的數據使用複製的字符串。然後使用相同的索引來操作原始字符串。

int startIndex = yourCopiedString.toLowerCase().indexOf("<sensdata>")+10; 
int endIndex = yourCopiedString.toLowerCase().indexOf("</sensdata>"); 
String dataPart = yourCopiedString.substring(startIndex, endIndex); 

對於替換:

String newEncodedString = yourOriginalString.replace(yourOriginalString.substring(startIndex,endIndex), "XXXXXXXX"); 

如果有給定的子串的重複然後嘗試。

String newEncodedString = yourOriginalString.substring(0,startIndex)+ "XXXXXXXX"+yourOriginalString.substring(endIndex,yourOriginalString.lenght); 
0

試試這個

String str = "<Record><op>Add</op><sensdata>400188711111</sensdata><id>4</id><a1>1111201090467034</a1></Record>" 

int start = str.indexOf("<sensdata>"); 
int end = str.indexOf("</sensdata>"); 
String sendDataVal = str.substring(start+10, end); 
2

您還可以使用XPath表達式來獲取所需的數據。

因此,該解決方案可能是:

String xml = "<Record><op>Add</op><sensdata>400188711111</sensdata><id>4</id>" 
      + "<a1>1111201090467034</a1></Record>"; 
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); 
    DocumentBuilder builder = null; 
    try { 
     builder = builderFactory.newDocumentBuilder(); 
    } catch (ParserConfigurationException e) { 
     e.printStackTrace(); 
    } 
    Document document = builder.parse(new ByteArrayInputStream(xml.getBytes())); 
    XPath xPath = XPathFactory.newInstance().newXPath(); 
    String data = xPath.compile("/Record/sensdata").evaluate(document); 
    System.out.println(data); 
-1

兩條路走在這裏,我想:如果這是不太可能這個任務將會改變,一個簡單的正則表達式可以爲您解決和一行代碼。如果問題變得結構化,或者您的應用程序使用大量xml,您可能需要解析它並動態運行。

一個簡單的正則表達式可以是:

".*<[sS]ensdata>(.*)</[sS]ensdata>.*", group 1 ("$1") will be your value. 
相關問題