2013-03-25 68 views
0

XML文件內容前,在XML文件中的字符串

<distributionChannels><distributionChannel type="Wap" id="1"><contentChannelRefs> 
<contentChannelRef id="2"><categories><category 
link="http://images/11.gif" id="1"><names><name lang="de">Top Downloads</name><name 
lang="ww">Tops</name></names></category></categories></contentChannelRef> 
</contentChannelRefs></distributionChannel> 
</distributionChannels> 

如何刪除對此我從一個XML文件和輸出讀取不需要的內容後刪除不需要的字符串應該看看下面的圖所示:

<category link="http://images/11.gif" id="1"><names><name lang="de">Top Downloads</name><name lang="ww">Tops</name></names></category> 

回答

3

可靠的解決方案 - 使用XML解析器。如果你想讀的類別逐一使用正則表達式

Matcher m = Pattern.compile("<category.*?>.*?</category>").matcher(xml); 
    for(int i = 0; m.find(); i++) { 
     System.out.println(m.group()); 
    } 
+0

感謝dorofeev,如果我有多個在相同的字符串我如何解決... – srp 2013-03-25 14:19:50

+0

好吧,看到更新後的版本 – 2013-03-25 14:34:21

+0

謝謝你,我的意思是我想提取僅,可以說如果我有100個,我正在寫50到第一個文件和下一個50到第二個文件。 – srp 2013-03-25 14:39:40

2

模式與XML的匹配,不建議簡單的辦法就是

s = s.substring(s.indexOf("<categories>"), s.indexOf("</categories>") + 13); 

。使用解析器來獲取節點並相應地管理它們。如果你有興趣打印它們,我已經包含了打印節點的代碼。

public static void main(String[] args) 
     throws ParserConfigurationException, SAXException, 
     IOException, XPathExpressionException { 
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); 
    domFactory.setNamespaceAware(true); 
    DocumentBuilder builder = domFactory.newDocumentBuilder(); 
    Document doc = builder.parse(new InputSource(new StringReader(s))); 

    XPathFactory factory = XPathFactory.newInstance(); 
    XPath xpath = factory.newXPath(); 
    XPathExpression expr 
      = xpath.compile("//categories//category"); 

    Object result = expr.evaluate(doc, XPathConstants.NODESET); 
    NodeList nodes = (NodeList) result; 
    //This is where you are printing things. You can handle differently if 
    //you would like. 
    for (int i = 0; i < nodes.getLength(); i++) { 
     System.out.println(nodeToString(nodes.item(i))); 
    } 
} 

private static String nodeToString(Node node) { 
    StringWriter sw = new StringWriter(); 
    try { 
     Transformer t = TransformerFactory.newInstance().newTransformer(); 
     t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); 
     t.setOutputProperty(OutputKeys.INDENT, "yes"); 
     t.transform(new DOMSource(node), new StreamResult(sw)); 
    } catch (TransformerException te) { 
     te.printStackTrace(); 
    } 
    return sw.toString(); 
}