2013-12-12 60 views
0

我的任務是修改包含RSS源的字符串。它有它的元素。我需要修改這些鏈接元素,然後輸出整個事情。我試過使用Documentbuilder,但每次嘗試修改節點時都會刪除所有後代節點。如何修改XML文件的元素然後打印整個東西

任何人都可以提出一個簡單的方法來檢索和修改這些節點,然後打印整個飼料。

public Document XMLParser(String rssFeed){ 
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); 
    DocumentBuilder docBuilder = null; 
    String nodeContents = null; 
    String newXML = ""; 
    try { 
     docBuilder = docFactory.newDocumentBuilder(); 
     Document doc = docBuilder.parse(new InputSource(new ByteArrayInputStream(rssFeed.getBytes("utf-8")))); 

     Node node = doc.getFirstChild(); 
     NodeList list = node.getChildNodes(); 
     NodeList nodeList = doc.getElementsByTagName("*"); 

     for (int i = 0; i < nodeList.getLength(); i++) { 
      Node curNode = nodeList.item(i); 
      if ("link".equals(curNode.getNodeName()) || "channel".equals(curNode.getNodeName())) { 
       nodeContents = curNode.getTextContent(); 
       nodeContents = "new contents"; 
       curNode.setTextContent(nodeContents); 
      } 
     } 
     return doc; 

    }catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 

RSS樣本:

<?xml version="1.0" encoding="UTF-8"?> 
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"> 
<channel> 
    <title>title for the channel</title> 
    <link><![CDATA[www.whatever.com]]></link> 
    <description><![CDATA[description of the channel.]]></description> 
    <item> 
     <title><![CDATA[title of the link]]></title> 
     <description><![CDATA[description of the link]]></description> 
     <link><![CDATA[www.whatever.com]]></link> 
     <enclosure url="thepictureURL" length="21830" type="image/png" /> 
     <pubDate>Thu, 01 Jan 2000 00:00:00 EDT</pubDate> 
    </item> 
</channel> 
</rss> 
+0

向我們展示您的代碼(刪除後代節點的代碼) – rzymek

+0

好的,我添加了有問題的代碼。 – popbottlepirate

+0

還包括您的RSS數據樣本。 – rzymek

回答

1

當心setTextContent(text)。如果您在具有子節點的節點上調用它,這些將被替換爲text

如果RSS不是太大,可以將其加載到內存中 - 將其解析爲DOM。修改<link>節點的內容。然後將DOM序列化回字符串:

public static String processLinks(String rssFeed) throws Exception { 
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); 
    DocumentBuilder builder = docFactory.newDocumentBuilder(); 
    Document doc = builder.parse(new InputSource(new StringReader(rssFeed))); 

    NodeList nodeList = doc.getElementsByTagName("link"); 
    for (int i = 0; i < nodeList.getLength(); i++) { 
    Node link = nodeList.item(i); 
    String value = link.getTextContent(); 
    //Do the processing. For example: 
    if(!value.startsWith("http://")) { 
     link.setTextContent("http://"+value); 
    } 
    } 
    return toString(doc); 
} 

private static String toString(Document doc) throws Exception { 
    TransformerFactory tf = TransformerFactory.newInstance(); 
    Transformer transformer = tf.newTransformer(); 
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); 
    StringWriter writer = new StringWriter(); 
    transformer.transform(new DOMSource(doc), new StreamResult(writer)); 
    return writer.toString(); 
} 
+0

甜美!謝謝,我會在今晚回家之前看看我是否可以試試。 – popbottlepirate

+0

這似乎是問題所在。感謝您的幫助1 – popbottlepirate

相關問題