Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java对现有xml的写入_Java_Xml_Xml Parsing - Fatal编程技术网

java对现有xml的写入

java对现有xml的写入,java,xml,xml-parsing,Java,Xml,Xml Parsing,我正在尝试将一些数据添加到现有的xml文件中。这是我所做的: try { String filepath = "/askhsh3/WebContent/askisi3.xml"; DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); Document

我正在尝试将一些数据添加到现有的xml文件中。这是我所做的:

try {
    String filepath = "/askhsh3/WebContent/askisi3.xml";
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(filepath);

    // Get the root element
    Node rootn = doc.getFirstChild();

    //Node staff = doc.getElementsByTagName("staff").item(0);

    // append a new node to staff
    Document doc2 = docBuilder.newDocument();
    Element patient = doc2.createElement("patient");

    Element st_as = doc.createElement("stoixeia_astheni");
    for(int i=1;i<=9;i++){
        Element tmp= doc2.createElement(elem[i]);
        tmp.appendChild(doc.createTextNode("aaa"));
        st_as.appendChild(tmp);
    }

    patient.appendChild(st_as);
    rootn.appendChild(patient);
    doc.appendChild(rootn);

} catch (ParserConfigurationException pce) {
    pce.printStackTrace();
} catch (IOException ioe) {
    ioe.printStackTrace();
} catch (SAXException sae) {
    sae.printStackTrace();
}
试试看{
字符串filepath=“/askhsh3/WebContent/askisi3.xml”;
DocumentBuilderFactory docFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder=docFactory.newDocumentBuilder();
Document doc=docBuilder.parse(文件路径);
//获取根元素
节点rootn=doc.getFirstChild();
//节点staff=doc.getElementsByTagName(“staff”)。项(0);
//将新节点附加到staff
Document doc2=docBuilder.newDocument();
元素patient=doc2.createElement(“患者”);
元素st_as=doc.createElement(“stoixeia_-sifi”);

对于(int i=1;i嗯,您正在修改内存中的xml文档,但是您没有将其保存回文件,而且这不会自动发生。您需要显式地编写它。下面是一些示例代码:

TransformerFactory tf = TransformerFactory.newInstance();
Transformer trans = tf.newTransformer();
OutputStream out = new FileOutputStream(filepath);
trans.transform(new DOMSource(doc), new StreamResult(out));

@MBanerjee xml文件没有任何更改..这是一个在eclipse中构建的web项目,askhsh3是项目的名称,
/workspace/askhsh/WebContent/askisi3.xml
如何更改文件的权限?我正在eclipse中运行tomcat,路径与project@user2420697对此不确定,您可以单独询问stion。此外,您还应该指定您得到的错误。