2012-05-04 19 views

答えて

2

DOMは、プロセスメモリのみに変更されたファイルのOutputStreamのにもTFormer.transformを呼び出す必要があり

package com.sumit.xml.client; 

import java.io.*; 

import org.apache.xml.serialize.OutputFormat; 
import org.apache.xml.serialize.XMLSerializer; 
import org.w3c.dom.*; 
import org.xml.sax.*; 
import javax.xml.parsers.*; 
import javax.xml.transform.*; 
import javax.xml.transform.dom.DOMSource; 
import javax.xml.transform.stream.StreamResult; 

public class ReadCDATA { 
    static public void main(String[] arg) { 
    try{ 
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); 
    System.out.print("Enter a XML file name: "); 
    String xmlFile = bf.readLine(); 
    File file = new File(xmlFile); 
    System.out.print("Enter an element which have to delete: "); 
    String remElement = bf.readLine(); 
    if (file.exists()){ 
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
    DocumentBuilder builder = factory.newDocumentBuilder(); 
    Document doc = builder.parse(xmlFile); 
    TransformerFactory tFactory = TransformerFactory.newInstance(); 
    Transformer tFormer = tFactory.newTransformer(); 
    Element element = (Element)doc.getElementsByTagName(remElement).item(0); 
// Remove the node 
    element.getParentNode().removeChild(element); 
// Normalize the DOM tree to combine all adjacent nodes 
    doc.normalize(); 
    Source source = new DOMSource(doc); 
    Result dest = new StreamResult(System.out); 
    tFormer.transform(source, dest); 
    System.out.println(); 

    } 
    else{ 
    System.out.println("File not found!"); 
    } 
    } 
    catch (Exception e){ 
    System.err.println(e); 
    System.exit(0); 
    } 
    } 
} 
0

おかげで... ..助けてください。あなたが示したコードには、基礎となるXMLファイルに透過的にデータを書き込むものは何もありません。

あなたは、XMLからノードを削除した後

createOutputFile("output.xml", transformToString(document)); 

private static String transformToString(Document document) { 
    try { 
     TransformerFactory transFactory = TransformerFactory.newInstance(); 
     Transformer transformer = transFactory.newTransformer(); 
     StringWriter buffer = new StringWriter(); 
     transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); 
     transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 
     transformer.transform(new DOMSource(document), new StreamResult(buffer)); 
     return buffer.toString(); 
    } catch (TransformerException e) { 
     throw new RuntimeException(e); 
    } 
} 


private static void createOutputFile(String filePath, String content) { 
    FileWriter writer = null; 
    try { 
     try { 
      writer = new FileWriter(filePath); 
      writer.write(content); 
     } finally { 
      if (writer != null) { 
       writer.flush(); 
       writer.close(); 
      } 
     } 
    } catch (IOException e) { 
     throw new RuntimeException(e); 
    } 
} 
0

のようなものを追加更新されますxmlファイルxmlファイルに内容を書き込んだ後、xmlファイル

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
    DocumentBuilder db = dbf.newDocumentBuilder(); 
    InputSource is = new InputSource(); 
    is.setCharacterStream(new StringReader(xml)); 

    Document doc = db.parse(is); 


// write the content into xml file 

    TransformerFactory transformerFactory = TransformerFactory.newInstance(); 
    Transformer transformer = transformerFactory.newTransformer(); 
    DOMSource source = new DOMSource(doc); 
    StreamResult result = new StreamResult(new File(filepath)); 
    transformer.transform(source, result); 

にコンテンツを記述する必要があります。

0
 TransformerFactory transformerFactory = TransformerFactory.newInstance(); 
     Transformer transformer = transformerFactory.newTransformer(); 
     DOMSource source = new DOMSource(doc); 
     String filepath = "/java/CarsNEW.xml"; //according to yuor path and file name 
     StreamResult result = new StreamResult(new File(filepath)); 
     transformer.transform(source, result);