2014-10-07 84 views
1

一些节点,我需要在这样格式化的XML文件中提取一些节点:提取从XML文件

<collection sentiment="negativo"> 
<comment> 
    <sentiment> ...</sentiment> 
    <chars>...</chars> 
    <words>...</words> 
    <text>blabla</text> 
    <lang>english</lang> 
    </comment> 

现在假设有其他<comment>元素系列,同样的XML文件中有<lang>spanish</lang>。 我需要创建两个单独的XML文件。第一个与具有孩子<lang>english</lang>(让我们eng.xml称呼它),并具有<lang>spanish</lang>第二个所有节点(姑且称之为spa.xml)

这里是我的Java代码:

public void getEnglishRows() throws IOException{ 
    OutputStreamWriter f = new OutputStreamWriter(new FileOutputStream("C:/eclipse/neg_eng.xml")); 
    BufferedWriter buff; 

    NodeList current_row = doc.getElementsByTagName("comment"); //Mette in una lista tutti i nodi row (che contengono a loro volta degli elementi) 
    NodeList tmp; 
    Node nodo = null; 

    buff = new BufferedWriter(f); 
    for(int i=0;i< current_row.getLength();i++){ 
     tmp = current_row.item(i).getChildNodes(); 
     for(int k=0;k<tmp.getLength();k++){ 
      nodo = tmp.item(k); 

      if("english".equals(nodo.getTextContent())) 
       System.out.println("IF ENGLISH"); 
       buff.write(current_row.item(i).getNodeValue());       
     } 
    } 


    buff.close(); 
} 

我不知道我是否清楚,我希望如此。

所以我有一个Xml文件,其中LOTR为<comment></comment>。我必须从这个全部<comment></comment>中提取出具有<lang>english</lang>的数据,并将节点(使用子节点)写入另一个XML文件。 <lang>spanish</lang>的行为相同。

eng.xml的输出是:

<comment> 
    <sentiment> ...</sentiment> 
    <chars>...</chars> 
    <words>...</words> 
    <text>blabla</text> 
    <lang>english</lang> 
    </comment> 

spa.xml的输出是:

<comment> 
     <sentiment> ...</sentiment> 
     <chars>...</chars> 
     <words>...</words> 
     <text>blabla</text> 
     <lang>spanish</lang> 
     </comment> 

我希望我是清楚的。我的问题是我可以提取所有节点的文本,但它不支持XML标记!

请帮帮我!

+0

调查jaxb或xstream – jgr208 2014-10-07 15:49:44

+0

好的,我会深入研究这一点。我还想要一些代码示例:) 非常感谢!编辑:Xstrem只是将对象序列化为XML,我已经有了一个XML。 – Leo91 2014-10-07 16:02:12

回答

0

为什么不尝试删除非英语评论? 所以我的建议是搜索标签并检测非英文标签。然后转到包含节点(元素)的父元素并将其删除。这保留了原始文件结构。

试试看看这个代码。它的工作对我来说:)

public void getEnglishRows() throws IOException, SAXException, ParserConfigurationException, TransformerException{  
    OutputStreamWriter f = new OutputStreamWriter(new FileOutputStream("./eng_sent.xml")); 
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
    DocumentBuilder db = dbf.newDocumentBuilder(); 
    Document doc = db.parse(new FileInputStream("C:/eclipse/neg_eng.xml")); 

    NodeList current_row = doc.getElementsByTagName("lang"); // search for the lang element 

    for(int i=0;i< current_row.getLength();i++){    
     String lang = current_row.item(i).getTextContent(); 

     if (!lang.equalsIgnoreCase("english")) { 
      // delete not english comment 
      Element comment = (Element) current_row.item(i).getParentNode(); 
      doc.getDocumentElement().removeChild(comment); 
      doc.normalize(); 
     }   
    } 

    // write the content into xml file 
    TransformerFactory transformerFactory = TransformerFactory.newInstance(); 
    Transformer transformer = transformerFactory.newTransformer(); 
    DOMSource source = new DOMSource(doc); 
    StreamResult result = new StreamResult(f); 
    transformer.transform(source, result);  
} 

文件neg_eng就会出现类似如下:

<collection sentiment="negativo"> 
<comment> 
    <sentiment> ...</sentiment> 
    <chars>...</chars> 
    <words>...</words> 
    <text>eng3</text> 
    <lang>english</lang> 
</comment> 
<comment> 
    <sentiment> ...</sentiment> 
    <chars>...</chars> 
    <words>...</words> 
    <text>eng1</text> 
    <lang>english</lang> 
</comment> 
<comment> 
    <sentiment> ...</sentiment> 
    <chars>...</chars> 
    <words>...</words> 
    <text>eng2</text> 
    <lang>english</lang> 
</comment> 

在原始的XML文件是:

<collection sentiment="negativo"> 
<comment> 
    <sentiment> ...</sentiment> 
    <chars>...</chars> 
    <words>...</words> 
    <text>eng3</text> 
    <lang>english</lang> 
</comment> 
<comment> 
    <sentiment> ...</sentiment> 
    <chars>...</chars> 
    <words>...</words> 
    <text>spa2</text> 
    <lang>spanish</lang> 
</comment> 
<comment> 
    <sentiment> ...</sentiment> 
    <chars>...</chars> 
    <words>...</words> 
    <text>eng1</text> 
    <lang>english</lang> 
</comment> 
<comment> 
    <sentiment> ...</sentiment> 
    <chars>...</chars> 
    <words>...</words> 
    <text>eng2</text> 
    <lang>english</lang> 
</comment> 
<comment> 
    <sentiment> ...</sentiment> 
    <chars>...</chars> 
    <words>...</words> 
    <text>spa1</text> 
    <lang>spanish</lang> 
</comment> 

希望提供s会帮助你! 快乐黑客;-)

+0

谢谢Rob!我今天晚上试试! +100! HH! – Leo91 2014-10-08 06:32:44