📄 testjdom.java
字号:
package com.ideas.test;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2003</p> * <p>Company: </p> * @author not attributable * @version 1.0 */import org.jdom.*;import org.jdom.output.*;import org.jdom.input.*;import java.io.*;public class TestJDOM{ public static void main(String args[])throws Exception{ SAXBuilder sb = new SAXBuilder(); //从文件构造一个Document,因为XML文件中已经指定了编码,所以这里不必了 Document doc = sb.build(new FileInputStream("c:\\conf.xml")); //加入一条处理指令 //ProcessingInstruction pi = new ProcessingInstruction // ("xml-stylesheet","href=\"bookList.html.xsl\" type=\"text/xsl\""); // doc.addContent(pi); Element root = doc.getRootElement(); //得到根元素 //java.util.List configuration = root.getChildren(); //得到根元素所有子元素的集合 //Element configuration = (Element)root.getChild("login"); //得到第一个book元素 //为第一本书添加一条属性 //Element attr = new Element("where"); //Attribute b = new Attribute("capital","peking"); //attr.addAttribute(b); //attr.addContent("add"); //configuration.addChild(attr); Element login = root.getChild("login"); //得到指定的字元素 //java.util.List logins = login.getChildren(); //Element login1 = (Element)logins.get(0); Element username = login.getChild("username"); username.setText("success1"); //author.setText("王五"); //将作者改为王五 //或 Text t = new Text("王五");book.addContent(t); //Element price = configuration.getChild("price"); //得到指定的字元素 //修改价格,比较郁闷的是我们必须自己转换数据类型,而这正是JAXB的优势 //price.setText(Float.toString(50.0f)); String indent = " "; boolean newLines = true; //支持中文所必须的 XMLOutputter outp = new XMLOutputter(indent,newLines,"GBK"); outp.output(doc, new FileOutputStream("c:\\conf.xml")); }};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -