当前位置: 技术问答>java相关
谁告诉我如何使用java创建,分析,修改一个xml,给我例子
来源: 互联网 发布时间:2015-04-21
本文导语: 我修改,分析知道了,但创建难道除了用Dom写没有更好的办法???? | import java.io.*; import org.jdom.*; import org.jdom.input.*; import org.jdom.output.*; public class JDOMTest { public static void main(String...
我修改,分析知道了,但创建难道除了用Dom写没有更好的办法????
|
import java.io.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
public class JDOMTest
{
public static void main(String[] args)
{
// root element
Element elementBook=new Element("Book"); // org.jdom.Element
Document doc=new Document(elementBook); // org.jdom.Document
Attribute atbt=new Attribute("Web","www.china-pub.com");
elementBook.setAttribute(atbt);
//elementBook.addAttribute("Web","www.china-pub.com"); // add an attribute
Comment cmt=new Comment("ÕâÊÇÎÒÓÃJDOMдµÄÒ»¸öXMLÎĵµ");
elementBook.addContent(cmt); // add a comment
// children element "Name"
Element elementName=new Element("Name");
elementName.addContent("Thinking In Java 2E");
elementBook.addContent(elementName);
// children element "Language"
Element elementLanguage=new Element("Language");
elementLanguage.addContent("English");
elementBook.addContent(elementLanguage);
// children element "Version"
Element elementVersion=new Element("Version");
elementVersion.addContent("2");
elementBook.addContent(elementVersion);
// children element "Author"
Element elementAuthor=new Element("Author");
elementAuthor.addContent("Bruce Eckel");
Attribute atbt2=new Attribute("Nation","USA");
elementAuthor.setAttribute(atbt2);
elementBook.addContent(elementAuthor);
// create an xml file
try
{
File f1=new File("book.xml");
XMLOutputter xo=new XMLOutputter(" ",true,"GB2312");
FileWriter fw=new FileWriter(f1);
xo.output(doc,fw);
fw.close();
File f2=new File("book2.xml");
DOMBuilder db=new DOMBuilder();
doc=db.build(f2);
fw=new FileWriter(f2);
xo.output(doc,fw);
fw.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
public class JDOMTest
{
public static void main(String[] args)
{
// root element
Element elementBook=new Element("Book"); // org.jdom.Element
Document doc=new Document(elementBook); // org.jdom.Document
Attribute atbt=new Attribute("Web","www.china-pub.com");
elementBook.setAttribute(atbt);
//elementBook.addAttribute("Web","www.china-pub.com"); // add an attribute
Comment cmt=new Comment("ÕâÊÇÎÒÓÃJDOMдµÄÒ»¸öXMLÎĵµ");
elementBook.addContent(cmt); // add a comment
// children element "Name"
Element elementName=new Element("Name");
elementName.addContent("Thinking In Java 2E");
elementBook.addContent(elementName);
// children element "Language"
Element elementLanguage=new Element("Language");
elementLanguage.addContent("English");
elementBook.addContent(elementLanguage);
// children element "Version"
Element elementVersion=new Element("Version");
elementVersion.addContent("2");
elementBook.addContent(elementVersion);
// children element "Author"
Element elementAuthor=new Element("Author");
elementAuthor.addContent("Bruce Eckel");
Attribute atbt2=new Attribute("Nation","USA");
elementAuthor.setAttribute(atbt2);
elementBook.addContent(elementAuthor);
// create an xml file
try
{
File f1=new File("book.xml");
XMLOutputter xo=new XMLOutputter(" ",true,"GB2312");
FileWriter fw=new FileWriter(f1);
xo.output(doc,fw);
fw.close();
File f2=new File("book2.xml");
DOMBuilder db=new DOMBuilder();
doc=db.build(f2);
fw=new FileWriter(f2);
xo.output(doc,fw);
fw.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
|
not really, you can always write XML in a string and then load it into a DOM object or output it to a file directly