1)首先建立个java project,XmlSample
2)将jdom.jar考到XmlSample工程中lib文件夹中
3)在Eclipse中的XmlSample->JRE System Library 上右键,Build Path->Configure Build Path->Add External JARs,找到你刚刚考过来的jdom.jar,确定
4)在XmlSample的Src目录中新建一个类,TestXML ,功能是:按照XML格式新建一个xml,并将xml文件转换成string类型。
xml格式要求如下:
xml格式
<?xml version="1.0" encoding="UTF-8"?>
<invoke type="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="invoke.xsd">
<rootin type="com.crea.im.ws.GIS"/>
<parameters>
<property name="User">GISUser</property>
<property name="Key">987654321</property>
<property name="SyncSID">TB001</property>
</parameters>
<field name="dtl_Description" type="StringArray" >
<E value="成功!" />
<E value="空间信息格式错误,或者其他错误提示!" />
</field>
</invoke>
<invoke type="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="invoke.xsd">
<rootin type="com.crea.im.ws.GIS"/>
<parameters>
<property name="User">GISUser</property>
<property name="Key">987654321</property>
<property name="SyncSID">TB001</property>
</parameters>
<field name="dtl_Description" type="StringArray" >
<E value="成功!" />
<E value="空间信息格式错误,或者其他错误提示!" />
</field>
</invoke>
TestXML 的代码如下:
TestXML
import java.io.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
public class TestXML
{
public static void main(String[] args)
{
try
{
String aString = BuildXMLDoc();
System.out.println(aString);
}
catch (Exception e)
{
System.out.println(e);
}
}
private static String BuildXMLDoc() throws IOException,JDOMException
{
//定义Element和Document
Element invoke, rootin, parameters, property, field, E;
Document Doc;
//设置xml头
Namespace ns_xsi = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
Attribute att = new Attribute("noNamespaceSchemaLocation", "invoke.xsd", ns_xsi);
//设置根目录invoke
invoke = new Element("invoke");
invoke.addNamespaceDeclaration(ns_xsi);
invoke.setAttribute(att);
Doc = new Document(invoke);
//设置一级目录rootin
rootin = new Element("rootin");
rootin.setAttribute("type","com.crea.im.ws.GIS");
//将目录rootin包含到根目录invoke中
invoke.addContent(rootin);
//设置二级目录parameters
parameters = new Element("parameters");
//将目录parameters包含到根目录invoke中
invoke.addContent(parameters);
//设置三级目录property(User)
property = new Element("property");
property.setAttribute("name","User");
property.setText("GISUser");
//将目录parameters包含到目录parameters
parameters.addContent(property);
//设置三级目录property(Key)
property = new Element("property");
property.setAttribute("name","Key");
property.setText("987654321");
//将目录parameters包含到目录parameters
parameters.addContent(property);
//设置三级目录property(SyncSID)
property = new Element("property");
property.setAttribute("name","SyncSID");
property.setText("TB001");
//将目录parameters包含到目录parameters
parameters.addContent(property);
//设置二级目录field
field = new Element("field");
field.setAttribute("name","dtl_SingleProjectCode");
field.setAttribute("type","StringArray");
//将目录field包含到根目录invoke中
invoke.addContent(field);
//设置三级目录E
E= new Element("E");
E.setAttribute("value","成功");
//将目录E包含到目录field
field.addContent(E);
//设置三级目录E
E= new Element("E");
E.setAttribute("value","空间信息格式错误,或者其他错误提示!");
//将目录E包含到目录field
field.addContent(E);
//将document输出成string格式
XMLOutputter XMLOut = new XMLOutputter();
Format format = Format.getPrettyFormat(); // 格式化文档
format.setEncoding("GBK"); // 由于默认的编码是utf-8,中文将显示为乱码,所以设为gbk
XMLOut.setFormat(format);
//将xml输出成string
StringWriter out = new StringWriter();
XMLOut.output(Doc, out);
String ImportState = out.toString();
//将xml输出到本地的c:\\sys.xml中
XMLOutputter XMLOut1 = new XMLOutputter();
XMLOut.output(Doc, new FileWriter("d:\\sys.xml"));
//返回xml字符串
return ImportState;
}
}
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
public class TestXML
{
public static void main(String[] args)
{
try
{
String aString = BuildXMLDoc();
System.out.println(aString);
}
catch (Exception e)
{
System.out.println(e);
}
}
private static String BuildXMLDoc() throws IOException,JDOMException
{
//定义Element和Document
Element invoke, rootin, parameters, property, field, E;
Document Doc;
//设置xml头
Namespace ns_xsi = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
Attribute att = new Attribute("noNamespaceSchemaLocation", "invoke.xsd", ns_xsi);
//设置根目录invoke
invoke = new Element("invoke");
invoke.addNamespaceDeclaration(ns_xsi);
invoke.setAttribute(att);
Doc = new Document(invoke);
//设置一级目录rootin
rootin = new Element("rootin");
rootin.setAttribute("type","com.crea.im.ws.GIS");
//将目录rootin包含到根目录invoke中
invoke.addContent(rootin);
//设置二级目录parameters
parameters = new Element("parameters");
//将目录parameters包含到根目录invoke中
invoke.addContent(parameters);
//设置三级目录property(User)
property = new Element("property");
property.setAttribute("name","User");
property.setText("GISUser");
//将目录parameters包含到目录parameters
parameters.addContent(property);
//设置三级目录property(Key)
property = new Element("property");
property.setAttribute("name","Key");
property.setText("987654321");
//将目录parameters包含到目录parameters
parameters.addContent(property);
//设置三级目录property(SyncSID)
property = new Element("property");
property.setAttribute("name","SyncSID");
property.setText("TB001");
//将目录parameters包含到目录parameters
parameters.addContent(property);
//设置二级目录field
field = new Element("field");
field.setAttribute("name","dtl_SingleProjectCode");
field.setAttribute("type","StringArray");
//将目录field包含到根目录invoke中
invoke.addContent(field);
//设置三级目录E
E= new Element("E");
E.setAttribute("value","成功");
//将目录E包含到目录field
field.addContent(E);
//设置三级目录E
E= new Element("E");
E.setAttribute("value","空间信息格式错误,或者其他错误提示!");
//将目录E包含到目录field
field.addContent(E);
//将document输出成string格式
XMLOutputter XMLOut = new XMLOutputter();
Format format = Format.getPrettyFormat(); // 格式化文档
format.setEncoding("GBK"); // 由于默认的编码是utf-8,中文将显示为乱码,所以设为gbk
XMLOut.setFormat(format);
//将xml输出成string
StringWriter out = new StringWriter();
XMLOut.output(Doc, out);
String ImportState = out.toString();
//将xml输出到本地的c:\\sys.xml中
XMLOutputter XMLOut1 = new XMLOutputter();
XMLOut.output(Doc, new FileWriter("d:\\sys.xml"));
//返回xml字符串
return ImportState;
}
}
在TestXML上右键,run as java application,控制台打印出xml的内容,同时也会将xml文件输出到d:\\sys.xml中。