java编辑xml文件(java写xml文件)
java Eclipse 编辑 spring的配置文件XML 显示自动导入namespace怎么做到?
1.window-》perference-》MyEclipse-》Files and Editors-》XML-》XML Catalog
2.选中中间的 User Specificed Entries 点击Add
3.选择 File System 按钮 找到 你从 jar包里面解压出的 .xsd 文件或 .dtd 文件 这里试例为 添加的.xsd 文件
key type:选择 Schema laction
key:在自动生成的路径后面加上你插入的.xsd 文件的版本名称 如:/spring-beans-3.2.xsd
4.用的时候是这样:
在项目的src下面,点击src左击 NEW-》选中XML(Basic Templates)-》Next
5.点击Next
6、选择Select XML Catalog entry 找到你刚刚添加的后缀为.xsd文件
7.在Root Element 中找到你所用的根元素,如果你想在添加其他的后缀为.xsd文件 选择Add :
8.选择Specify New Namespace
Prefix:填写标签:这里的例子为context
Namespace Name:填写的是 Location Hint中的
Location Hint:这里我用的是:
9.点击OK -》再点击Finish 就完成了,到这整个添加.xsd文件就完成。
10.注解是找到后缀为.xsd 文件按以上步骤添加进去,编辑xml文件时就自动生成了。
JAVA如何写XML文件
import java.io.*;
import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
public class DOM4JTest {
public static void main(String[] args) {
Document doc = DocumentHelper.createDocument();
doc.addProcessingInstruction("xml-stylesheet", "type='text/xsl href='students.xsl'");
Element root = doc.addElement("students");
Element eltStu1 = root.addElement("student").addAttribute("sn", "01");
Element eltName1 = eltStu1.addElement("name");
Element eltAge1 = eltStu1.addElement("age");
eltName1.setText("张三");
eltAge1.setText("20");
Element eltStu2 = root.addElement("student").addAttribute("sn", "02");
Element eltName2 = eltStu2.addElement("name");
Element eltAge2 = eltStu2.addElement("age");
eltName2.setText("李四");
eltAge2.setText("18");
try {
OutputFormat format = new OutputFormat(" ", true);
format.setEncoding("gb2312");
// 可以把System.out改为你要的流。
XMLWriter xmlWriter = new XMLWriter(new PrintWriter(System.out), format);
xmlWriter.write(doc);
xmlWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
JAVA如何写XML文件?
import java.io.*;\x0d\x0a \x0d\x0aimport org.dom4j.*;\x0d\x0a import org.dom4j.io.OutputFormat;\x0d\x0a import org.dom4j.io.XMLWriter;\x0d\x0a \x0d\x0apublic class DOM4JTest {\x0d\x0a public static void main(String[] args) {\x0d\x0a Document doc = DocumentHelper.createDocument();\x0d\x0a doc.addProcessingInstruction("xml-stylesheet", "type='text/xsl href='students.xsl'");\x0d\x0a Element root = doc.addElement("students");\x0d\x0a \x0d\x0a Element eltStu1 = root.addElement("student").addAttribute("sn", "01");\x0d\x0a Element eltName1 = eltStu1.addElement("name");\x0d\x0a Element eltAge1 = eltStu1.addElement("age");\x0d\x0a eltName1.setText("张三");\x0d\x0a eltAge1.setText("20");\x0d\x0a \x0d\x0a Element eltStu2 = root.addElement("student").addAttribute("sn", "02");\x0d\x0a Element eltName2 = eltStu2.addElement("name");\x0d\x0a Element eltAge2 = eltStu2.addElement("age");\x0d\x0a eltName2.setText("李四");\x0d\x0a eltAge2.setText("18");\x0d\x0a \x0d\x0a try {\x0d\x0a OutputFormat format = new OutputFormat("\x0d\x0a ", true);\x0d\x0a format.setEncoding("gb2312");\x0d\x0a // 可以把System.out改为你要的流。\x0d\x0a XMLWriter xmlWriter = new XMLWriter(new PrintWriter(System.out), format);\x0d\x0a xmlWriter.write(doc);\x0d\x0a xmlWriter.close();\x0d\x0a } catch (IOException e) {\x0d\x0a e.printStackTrace();\x0d\x0a }\x0d\x0a }\x0d\x0a }
java修改XMl文件,请高手指点
楼主这个思路是没有什么问题的,存入map中可以方便整个项目使用,特别是针对那些项目中经常要读取的xml,可以使用xpath
public static void modifyXMLFile(String name,String value) {
String oldStr = "E:/Work/Proj/gfweb/src/configuration.xml";
String newStr = "E:/Work/Proj/gfweb/src/configuration.xml";
Document document = null;
try {
SAXReader saxReader = new SAXReader(); // 用来读取xml文档
saxReader.setEncoding("GBK");
document = saxReader.read(new File(oldStr)); // 读取xml文档
List list = document.selectNodes("/system/category/item");// 用xpath查找节点book的属性
Iterator iter = list.iterator();
while (iter.hasNext()) {
Element element = (Element) iter.next();
//Attribute attribute = (Attribute) iter.next();
String itemname = element.attributeValue("name");
if(name.equals(itemname)){
element.setAttributeValue("value",value);
}
}
} catch (Exception e) {
e.printStackTrace();
}
try {
OutputFormat outFmt = OutputFormat.createPrettyPrint();
outFmt.setEncoding("GBK");
outFmt.setIndent(true);
XMLWriter writer = new XMLWriter(new FileWriter(new File(newStr)),outFmt);
writer.write(document);
writer.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}