用jdom操作微软office xml

本文介绍了一种利用Java库JDOM创建特定格式XML文件的方法。该方法详细展示了如何定义XML的命名空间、属性及元素,并通过具体示例代码演示了整个过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

所用的xml格式如下

<FILEREF> <xml xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema"> <s:Schema id="RowsetSchema"> <s:ElementType name="row" content="eltOnly" rs:updatable="true"> <s:AttributeType name="Authority_No" rs:number="1" rs:write="true"> <s:datatype dt:type="string" rs:dbtype="str" dt:maxLength="50" rs:precision="0" rs:maybenull="false" /> </s:AttributeType> <s:ElementType> <xml> </FILEREF>

生成xml的java代码测试类,需要自己下载jdom的jar包

package testJDOM; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.jdom.Attribute; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.Namespace; import org.jdom.output.Format; import org.jdom.output.XMLOutputter; public class testJDOM { private static final Namespace ns_s = Namespace.getNamespace("s", "uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882"); private static final Namespace ns_dt = Namespace.getNamespace("dt","uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"); private static final Namespace ns_rs = Namespace.getNamespace("rs", "urn:schemas-microsoft-com:rowset"); private static final Namespace ns_z =Namespace.getNamespace("z","#RowsetSchema"); private static Attribute A_name = new Attribute("name",""); private static Attribute A_number = new Attribute("number","1"); private static Attribute A_write = new Attribute("write","true"); private static Attribute A_type = new Attribute("type","string"); private static Attribute A_dbtype = new Attribute("dbype","str"); private static Attribute A_maxLength = new Attribute("maxLength","50"); private static Attribute A_precision = new Attribute("precision","0"); private static Attribute A_maybenull = new Attribute("maybenull","false"); private static Attribute A_fixedlength = new Attribute("fixedlength","true"); public static void main(String[] args) throws JDOMException, IOException { A_number.setNamespace(ns_rs); A_write.setNamespace(ns_rs); A_type.setNamespace(ns_dt); A_dbtype.setNamespace(ns_rs); A_maxLength.setNamespace(ns_dt); A_precision.setNamespace(ns_rs); A_maybenull.setNamespace(ns_rs); // setNamespace(); //First method to create Document doc = new Document(); Element E_policy_data = new Element("POLICY_DATA"); //设定xml的验证文件 /*DocType arg0 = new DocType(temp ); doc.setDocType(arg0);*/ Element E_xml = new Element("xml"); E_xml.addNamespaceDeclaration(ns_s); E_xml.addNamespaceDeclaration(ns_dt); E_xml.addNamespaceDeclaration(ns_rs); E_xml.addNamespaceDeclaration(ns_z); doc.addContent(E_policy_data); Element E_policy_type = new Element("POLICY_TYPE"); E_policy_type.setText("1"); E_policy_data.addContent(E_policy_type); E_policy_data.addContent(E_xml); //Add E_schema Element E_Schema =new Element("Schema"); E_Schema.setNamespace(ns_s); E_Schema.setAttribute("id", "RowsetSchema"); E_xml.addContent(E_Schema); //Add Element type Element E_ElementType = new Element("ElementType"); E_ElementType.setNamespace(ns_s); Attribute A_row = new Attribute("name","row"); Attribute A_eltOnly = new Attribute("content","eltOnly"); Attribute A_updatable = new Attribute("updatable","true"); A_updatable.setNamespace(ns_rs); List<Attribute> Element_list = new ArrayList<Attribute>(); Element_list.add(A_row); Element_list.add(A_eltOnly); Element_list.add(A_updatable); E_ElementType.setAttributes(Element_list); E_Schema.addContent(E_ElementType); //Add AttributeType name="Authority" Element E_Authority_No = new Element("AttributeType"); E_ElementType.setNamespace(ns_s); //Attribute A_Authority_No = new Attribute("name","A_Authority_No"); A_name.setValue("Authority_No"); List<Attribute> Attribute_list = new ArrayList<Attribute>(); Attribute_list.add(A_name); Attribute_list.add(A_number); Attribute_list.add(A_write); E_Authority_No.setAttributes(Attribute_list); E_ElementType.addContent(E_Authority_No); // E_ElementType.addContent(E_Authority_No); //Add datatype Element E_datatype = new Element("datatype"); E_datatype.setNamespace(ns_s); List<Attribute> datatype_list = new ArrayList<Attribute>(); datatype_list.add(A_type); datatype_list.add(A_dbtype); datatype_list.add(A_maxLength); datatype_list.add(A_precision); datatype_list.add(A_maybenull); E_datatype.setAttributes(datatype_list); E_Authority_No.addContent(E_datatype); System.out.println(doc); Format f = Format.getPrettyFormat(); XMLOutputter XMLOut = new XMLOutputter(f); XMLOut.output(doc, new FileOutputStream("haha.xml")); } }

读取xml的代码相当简单,现在就先不列出来了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值