`
dato0123
  • 浏览: 939995 次
文章分类
社区版块
存档分类
最新评论

【转】用JDOM建立XML文件

 
阅读更多
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->importjavax.xml.parsers.*;
importjava.util.
*;
importjava.io.
*;
importorg.jdom.Document;
importorg.jdom.Element;
importorg.jdom.input.SAXBuilder;
importorg.jdom.output.XMLOutputter;

publicclassCreateXML
{
DocumentBuilderFactoryfactory
=null;
DocumentBuilderbuilder
=null;
org.w3c.dom.DocumentmyDocument
=null;
//创建XML文件
//要创建的XML名字和路进
publicvoidctrateXMlFile(Stringfile){
ElementcarElement
=newElement("web-app");//建立元素
DocumentmyDocument=newDocument(carElement);//建立一个文档并指定根元素
try{
XMLOutputteroutputter
=newXMLOutputter();
outputter.output(myDocument,System.
out);
FileWriterwriter
=newFileWriter(file);
outputter.output(myDocument,writer);
writer.close();
}
catch(java.io.IOExceptione){
e.printStackTrace();
}
}
//增加节点
//第1个参数:要增加节点的名字,第2个参数:要修改xml的路进名
publicvoidaddXMLNode(StringnodeName,StringxmlFile){
try{
Elementelement
=null;
SAXBuilderbuilder
=newSAXBuilder();
Documentdoc
=builder.build(newFile(xmlFile));
if(doc.getRootElement().getChild(nodeName)!=null){
System.
out.println("该节点以存在!");
}
else{
element
=newElement(nodeName);
doc.getRootElement().addContent(element);
XMLOutputterfmt
=newXMLOutputter();
fmt.output(doc,System.
out);
FileWriterwriter
=newFileWriter(xmlFile);
fmt.output(doc,writer);
writer.close();
}
}
catch(Exceptionex){
ex.printStackTrace();
}

}
//增加节点属性
//第1个参数:要增加属性的节点的名字,第2个参数:要增加属性的名字,第3个参数:属性的值,第4个参数:要修改xml的路进名
publicvoidsetXMLNodeAttribute(StringnodeName,Stringattribute,Stringvalue,StringxmlFile){
try{
SAXBuilderbuilder
=newSAXBuilder();
Documentdoc
=builder.build(newFile(xmlFile));
Elemente
=doc.getRootElement();
//System.out.println("a"+e.getChild("servlet"));
if(e.getChild(nodeName)==null){
System.
out.println("该节点不存在!");
}
else{
e.getChild(nodeName).setAttribute(attribute,value);
XMLOutputterfmt
=newXMLOutputter();
fmt.output(doc,System.
out);
FileWriterwriter
=newFileWriter(xmlFile);
fmt.output(doc,writer);
writer.close();
}
}
catch(Exceptionex){
ex.printStackTrace();
}
}
//增加接点内容
//第1个参数:要增加内容的节点的名字,第2个参数:要增加的内容,第3个参数:要修改xml的路进名
publicvoidsetXMLNodeContent(StringnodeName,Stringcontent,StringxmlFile){
try{
SAXBuilderbuilder
=newSAXBuilder();
Documentdoc
=builder.build(newFile(xmlFile));
Elemente
=doc.getRootElement();
//System.out.println("a"+e.getChild("servlet"));
if(e.getChild(nodeName)==null){
System.
out.println("该节点不存在!");
}
elseif(e.getChild(nodeName).getText().equals(content)){
System.
out.println("该节点内容以存在!");

}
else{
e.getChild(nodeName).addContent(content);
XMLOutputterfmt
=newXMLOutputter();
fmt.output(doc,System.
out);
FileWriterwriter
=newFileWriter(xmlFile);
fmt.output(doc,writer);
writer.close();
}
}
catch(Exceptionex){
ex.printStackTrace();
}
}
//增加子接点
//第1个参数:要增子节点的节点的名字,第2个参数:要增加的子节点的名字,第3个参数:要修改xml的路进名
publicvoidsetXMLChildNode(StringnodeName,StringchildName,StringxmlFile){
try{
SAXBuilderbuilder
=newSAXBuilder();
Documentdoc
=builder.build(newFile(xmlFile));
Elemente
=doc.getRootElement();
if(e.getChild(nodeName)==null){
System.
out.println("该节点不存在!");
}
elseif(e.getChild(nodeName).getChild(childName)!=null){
System.
out.println("该子节点以存在!");
}
else{
Elementchild
=newElement(childName);
e.getChild(nodeName).addContent(child);
XMLOutputterfmt
=newXMLOutputter();
fmt.output(doc,System.
out);
FileWriterwriter
=newFileWriter(xmlFile);
fmt.output(doc,writer);
writer.close();
}
}
catch(Exceptionex){
ex.printStackTrace();
}
}
//增加子节点属性
//第1个参数:节点的名字,第2个参数:要增加属性的子节点的名字,第3个参数:属性的名字,第4个参数:属性的值,第4个参数:要修改xml的路进名
publicvoidsetXMLChildNodeAttribute(StringnodeName,StringchildName,Stringattribute,Stringvalue,StringxmlFile){
try{
SAXBuilderbuilder
=newSAXBuilder();
Documentdoc
=builder.build(newFile(xmlFile));
Elemente
=doc.getRootElement();
if(e.getChild(nodeName)==null){
System.
out.println("该节点不存在!");
}
elseif(e.getChild(nodeName).getChild(childName)==null){
System.
out.println("该子节点不存在!");
}
else{
e.getChild(nodeName).getChild(childName).setAttribute(attribute,value);
XMLOutputterfmt
=newXMLOutputter();
fmt.output(doc,System.
out);
FileWriterwriter
=newFileWriter(xmlFile);
fmt.output(doc,writer);
writer.close();
}
}
catch(Exceptionex){
ex.printStackTrace();
}
}
//增加子节点的内容
//第1个参数:节点的名字,第2个参数:要增加属性的子节点的名字,第3个参数:要增加的内容,第4个参数:要修改xml的路进名
publicvoidsetXMLChildNodeContent(StringnodeName,StringchildName,Stringcontent,StringxmlFile){
try{
SAXBuilderbuilder
=newSAXBuilder();
Documentdoc
=builder.build(newFile(xmlFile));
Elemente
=doc.getRootElement();
if(e.getChild(nodeName)==null){
System.
out.println("该节点不存在!");
}
elseif(e.getChild(nodeName).getChild(childName).getText().equals(content)){
System.
out.println("该子节点内容以存在!");
}
elseif(e.getChild(nodeName).getChild(childName)==null){
System.
out.println("该子节点不存在!");
}
else{
e.getChild(nodeName).getChild(childName).addContent(content);
XMLOutputterfmt
=newXMLOutputter();
fmt.output(doc,System.
out);
FileWriterwriter
=newFileWriter(xmlFile);
fmt.output(doc,writer);
writer.close();
}
}
catch(Exceptionex){
ex.printStackTrace();
}
}
//删除节点
//第1个参数:要删除的节点名字,第2个参数:要修改xml的路进名
publicvoidremoveXMLNode(StringnodeName,StringxmlFile){
try{
SAXBuilderbuilder
=newSAXBuilder();
Documentdoc
=builder.build(newFile(xmlFile));
Elemente
=doc.getRootElement();
if(e.getChild(nodeName)==null){
System.
out.println("该节点不存在!");
}
else{
e.removeChild(nodeName);
XMLOutputterfmt
=newXMLOutputter();
fmt.output(doc,System.
out);
FileWriterwriter
=newFileWriter(xmlFile);
fmt.output(doc,writer);
writer.close();
}
}
catch(Exceptionex){
ex.printStackTrace();
}
}
//删除子节点
//第1个参数:节点名字,第2个参数:要删除的子节点的名字,第3个参数:要修改xml的路进名
publicvoidremoveXMLChildNode(StringnodeName,StringchildName,StringxmlFile){
try{
SAXBuilderbuilder
=newSAXBuilder();
Documentdoc
=builder.build(newFile(xmlFile));
Elemente
=doc.getRootElement();
//System.out.println("a"+e.getChild("servlet"));
if(e.getChild(nodeName)==null){
System.
out.println("该节点不存在!");
}
elseif(e.getChild(nodeName).getChild(childName)==null){
System.
out.println("该子节点不存在!");
}
else{
e.getChild(nodeName).removeChild(childName);
XMLOutputterfmt
=newXMLOutputter();
fmt.output(doc,System.
out);
FileWriterwriter
=newFileWriter(xmlFile);
fmt.output(doc,writer);
writer.close();
}

}
catch(Exceptionex){
ex.printStackTrace();
}
}
publicstaticvoidmain(String[]args)throwsException{
CreateXMLxml
=newCreateXML();
//新建xml
xml.ctrateXMlFile("create.xml");
//增加节点
xml.addXMLNode("zhangbo3","create.xml");
//增加节点属性
xml.setXMLNodeAttribute("zhangbo3","name","zhangbo","create.xml");
//增加节点的内容
xml.setXMLNodeContent("zhangbo3","white-collar","create.xml");
//增加子节点
xml.setXMLChildNode("zhangbo3","mapping","create.xml");
//增加子节点的属性
xml.setXMLChildNodeAttribute("zhangbo3","mapping","name","struts-config.xml","create.xml");
//增加子节点的内容
xml.setXMLChildNodeContent("zhangbo3","mapping","helloword!","create.xml");
//删除节点
//xml.removeXMLNode("zhangbo3","create.xml");
//删除子节点
//xml.removeXMLChildNode("zhangbo3","mapping","create.xml");
}
}

分享到:
评论

相关推荐

    java利用jdom解析xml文件

    以上就是使用JDOM解析XML文件的基本流程和关键知识点。通过JDOM,你可以轻松地处理XML文档,无论是读取还是修改,都变得轻而易举。在实际开发中,掌握XML和JDOM的操作对于处理结构化数据和接口通信至关重要。

    java jdom解析xml

    - **构建Document对象**:首先,需要创建一个`DocumentBuilderFactory`实例,然后用它来建立`DocumentBuilder`,最后通过`DocumentBuilder`的`parse()`方法加载XML文件。 - **解析XML**:使用`Document`对象的`...

    jdom 读取,建立,修改 xml

    **JDOM建立XML** 创建XML文档时,首先需要创建`Element`对象来代表XML元素,然后通过添加子元素、属性等来构建XML结构。下面是一个创建简单XML文档的例子: ```java import org.jdom2.*; import org.jdom2.output....

    jdom从MYSQL导出XML文件

    标题“jdom从MYSQL导出XML文件”表明我们要讨论的是如何使用Java的JDOM库将数据从MySQL数据库导出到XML文件的过程。JDOM是一个专为Java设计的XML处理库,它提供了简单、高效的API来读写XML文档。MySQL则是一种广泛...

    jdom从数据库和xml拿数据生成xml

    标题中的“jdom从数据库和xml拿数据生成xml”指的是使用Java DOM(JDOM)库来处理XML数据,包括从数据库获取数据并将其转化为XML格式,以及从已有的XML文件中提取信息。JDOM是一个纯Java的XML API,它提供了一种高效...

    基于socket通信传递xml(jdom生成)文件的demo(maven管理)集成slf4j

    2. **Client端**:使用`Socket`连接到服务器,生成XML文件(利用JDOM API),将其序列化后写入Socket的输出流。 3. **XML处理**:这部分可能包含XML的创建、修改和验证,JDOM API的使用示例。 4. **Maven配置**:`...

    Java实现XML解析【JDom】

    要使用JDom解析XML文件,首先需要导入必要的库,如`org.jdom2.*`。然后,我们可以使用`SAXBuilder`类来构建XML文档的DOM表示: ```java import org.jdom2.Document; import org.jdom2.input.SAXBuilder; public ...

    XML解析技术-JDOM解析.doc

    1. 建立XML文档:除了解析XML文件,JDOM还可以用来创建新的XML文档。 2. 修改XML:你可以添加、删除或修改XML元素和属性,然后使用XMLOutputter将更改写回文件。 3. XSLT转换:JDOM可以与Java的XSLT API结合使用,...

    java操作xml和sql server 2008必备包(crimson jdom sqljdbc sqljdbc4)

    例如,一个常见的应用场景可能是:应用从XML文件中读取配置数据,然后使用JDBC驱动程序连接到SQL Server 2008数据库,将这些数据存储或检索到数据库中,实现数据的持久化。 在实际开发中,正确导入和使用这些库至关...

    JDOM实现从数据库读取字段生成XML树

    DOMBuilder则适用于小到中型的XML文件,它一次性加载整个文档到内存,便于快速访问。 要实现从数据库读取字段生成XML树,我们需要以下步骤: 1. **数据库连接**:首先,我们需要建立与数据库的连接。这里假设我们...

    自己用解析XML实例

    然后,你可以创建一个`SAXBuilder`对象来解析XML文件: ```java SAXBuilder builder = new SAXBuilder(); Document document = null; try { document = builder.build("path_to_xml_file.xml"); } catch (Exception...

    Java实现XML导入不同数据库,从数据库导出数据到XML

    1. **解析XML**:使用JDOM读取XML文件并将其转换为DOM(Document Object Model)对象。 2. **建立连接**:使用Java的JDBC(Java Database Connectivity)API建立与数据库的连接。 3. **创建SQL语句**:根据XML结构...

    从配置文件XML读取信息连接数据库

    首先,我们需要了解XML文件的基本结构。XML是一种标记语言,用于存储和传输数据。在我们的场景中,一个简单的XML配置文件可能如下所示: ```xml &lt;url&gt;jdbc:mysql://localhost:3306/mydb &lt;username&gt;root ...

    DWR.xml配置文件说明书(含源码)

    &lt;creator id="new" class="uk.ltd.getahead.dwr.create.NewCreator"/&gt;DWR已经将这配置到了内置的dwr.xml文件中,并不需要进行额外的配置. Creator通过调用类的默认的构造方法创建实例.应用new creator有以下几个优点:...

    dom4j、jdom、mysql-connector相关jar包

    这样,XML文件可以作为配置中心,存储数据库连接参数,而Java代码则负责动态读取这些配置并进行数据交互。 总的来说,DOM4J和JDOM提供了灵活的XML处理工具,它们各有优势,选择哪个取决于具体需求。而`mysql-...

    java解释Xml,从数据库中读出来,解释Xml

    XML文件通常用于存储配置信息、数据交换或Web服务中的数据传输。 2. **Java解析XML的API** - **DOM(Document Object Model)**:DOM是基于树形结构的API,它将整个XML文档加载到内存中形成一个节点树。通过遍历这...

    DOM4J创建XML是一个实例

    8. 性能考虑:虽然DOM4J提供了方便的操作XML的API,但DOM模型(Document Object Model)会将整个XML加载到内存中,对于大体积的XML文件可能会消耗大量内存。在处理大型XML时,可以考虑使用SAX或者StAX等基于事件的...

    Java与XML实现数据抽取

    通过利用Java的JDBC API来连接数据库,使用DOM/SAX/JDOM等工具来解析和生成XML文件,再结合GUI工具的设计,可以开发出一个功能强大且易于使用的数据抽取工具。这种工具的应用范围广泛,对于需要频繁处理大量异构数据...

    dom4j-1.6.1架包,读取xml

    5. 建立XML文档:DOM4J提供了简单的方法来创建新的XML文档或修改现有文档。例如,可以使用`DocumentFactory`创建新的`Document`,然后通过`Element`对象构建XML结构。 6. 功能丰富的API:DOM4J的API设计得非常友好...

Global site tag (gtag.js) - Google Analytics