「java保存xml」java保存xml 文件名

博主:adminadmin 2022-12-01 18:17:06 71

今天给各位分享java保存xml的知识,其中也会对java保存xml 文件名进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java如何读取一个文件夹并且保存在.xml中

你用File f=new File("D:\\TestResult\\" + folderName); boolean b=f.mkdirs(); 先创建文件夹,然后在创建文件夹下的输出文件 if(b) FileOutputStream fos=new FileOutputStream("D:\\TestResult\\" + folderName + xmlName + ".xml"));

java 添加节点并保存成XML问题

在java编程中,用dom4j的api来处理xml,很简单的,给段代码,需要导入dom4j.jar

Document doc = DocumentHelper.createDocument();

  //根节点

  Element rootEle=doc.addElement("root");

  Element ele1 = rootEle.addElement("ele1");

  ele1.addText("节点1");

  Element ele2 = rootEle.addElement("ele2");

  ele2.addText("节点2");

  System.out.println(doc.asXML());

如何用Java实现对xml文件的读取和写入以及保存

直接附源码import java.io.FileWriter;

import java.io.IOException;

import java.util.Iterator;import org.dom4j.*;

import org.dom4j.io.XMLWriter;

public class Dom4jSample { public static void main(String[] args) {

Dom4jSample dom4jSample = new Dom4jSample();

Document document = dom4jSample.createDocument();

try{

dom4jSample.FileWrite(document);

Document documentStr = dom4jSample.StringToXML("ChinaI Love!/China");

dom4jSample.XMLWrite(documentStr);

Element legend = dom4jSample.FindElement(document);

System.out.println(legend.getText());

}

catch(Exception e)

{

}

}

/*

* Create a XML Document

*/

public Document createDocument()

{

Document document = DocumentHelper.createDocument();

Element root = document.addElement("root");

Element author1 = root.addElement("Lynch");

author1.addAttribute("Age","25");

author1.addAttribute("Country","China");

author1.addText("I am great!");

Element author2 = root.addElement("Legend");

author2.addAttribute("Age","25");

author2.addAttribute("Country","China");

author2.addText("I am great!too!");

return document;

}

/*

* Create a XML document through String

*/

public Document StringToXML(String str) throws DocumentException

{

Document document = DocumentHelper.parseText(str);

return document;

}

public Element FindElement(Document document)

{

Element root = document.getRootElement();

Element legend = null;

for(Iterator i=root.elementIterator("legend");i.hasNext();)

{

legend = (Element)i.next();

}

return legend;

}

/*

* Write a XML file

*/

public void FileWrite(Document document) throws IOException

{

FileWriter out = new FileWriter("C:/Dom2jSample.xml");

document.write(out);

out.close();

}

/*

* Write a XML format file

*/

public void XMLWrite(Document document) throws IOException

{

XMLWriter writer = new XMLWriter(new FileWriter("C:/Dom2jSampleStr.xml"));

writer.write(document);

writer.close();

}

}

java解析XML修改特定项的值然后保存xml文件,求高手帮忙 急需 在线等

import java.io.File;

import java.io.FileInputStream;

import java.io.FileWriter;

import java.util.List;

import org.dom4j.Document;

import org.dom4j.Element;

import org.dom4j.io.SAXReader;

import org.dom4j.io.XMLWriter;

public static void main(String[] args) throws Exception {

SAXReader sax = new SAXReader();

//假设你的xml是放在D盘下的叫做a.xml的文件

File file = new File("D:\\a.xml");

Document doc = sax.read(new FileInputStream(file));

Element root = doc.getRootElement();

root = root.element("session-factory");

ListElement els = root.elements();

for(Element e : els){

if(e.getName().equals("property")){

String attval = e.attribute("name").getText();

if(attval.equals("hibernate.connection.url")){

//要修改的url的新值

e.setText("newUrl");

}else if(attval.equals("hibernate.connection.username")){

/ /要修改的username的新值

e.setText("newUsername");

}else if(attval.equals("hibernate.connection.password")){

//要修改的password的新值

e.setText("newPassword");

}

}

}

//重新写入到文件

XMLWriter output = new XMLWriter(new FileWriter(file));

output.write(doc);

output.close();

}

纯手打的,应该还有更简单的方法,但是没有工具忘记函数了,给你写了一个我记得住的方式,这个应该是可以的,用的是dom4j,如果你没有可能需要导入一个dom4j.jar的jar包

关于java保存xml和java保存xml 文件名的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-01,除非注明,否则均为首码项目网原创文章,转载请注明出处。