你的代码是正确的,在我这里可以正常运行的,输出结果为: <?xml version="1.0" encoding="UTF-8"?><CGeoML> <CGeoMLFileTitle> <cgDataMark>CNSDTF-CGML</cgDataMark> <cgVersion>1.0</cgVersion> <cgUnit>M</cgUnit> <cgDIM>2</cgDIM> <cgTopo>0</cgTopo> <cgcoordinate>M</cgcoordinate> <cgprojection /> <cgspheriod /> <cgParameters /> <cgScaleM></cgScaleM> <cgDate>2003</cgDate> <cgseparator>,</cgseparator> </CGeoMLFileTitle> </CGeoML> //////////////////////////////////////// import java.io.*; import org.w3c.dom.*; import org.xml.sax.*; import javax.xml.parsers.*; import org.apache.crimson.tree.XmlDocument;/** * <p>Title: china CGeoML</p> * <p>Description: </p> * * <p>Copyright: Copyright (c) 2003</p> * <p>Company: </p> * @author unascribed * @versionc 1.0 */public class WriteGML { private Document document; public WriteGML() { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try{ DocumentBuilder builder=factory.newDocumentBuilder(); document=builder.newDocument(); } catch(ParserConfigurationException pce){ pce.printStackTrace(); }catch(Exception e){ System.out.print(e);} Element root=document.createElement("CGeoML"); document.appendChild(root); Node CGeoMLFileTitle=createCGeoMLFileTitle(document); root.appendChild(CGeoMLFileTitle); try{ ((XmlDocument)document).write(new FileOutputStream("d:\\out.txt")); } catch(Exception ioe) { ioe.printStackTrace(); } } public Node createCGeoMLFileTitle(Document document) { Element cgDataMark=document.createElement("cgDataMark"); Element cgVersion=document.createElement("cgVersion"); Element cgUnit=document.createElement("cgUnit"); Element cgDIM=document.createElement("cgDIM"); Element cgTopo=document.createElement("cgTopo"); Element cgcoordinate=document.createElement("cgcoordinate"); Element cgprojection=document.createElement("cgprojection"); Element cgspheriod=document.createElement("cgspheriod"); Element cgParameters=document.createElement("cgParameters"); Element cgScaleM=document.createElement("cgScaleM"); Element cgDate=document.createElement("cgDate"); Element cgseparator=document.createElement("cgseparator"); cgDataMark.appendChild(document.createTextNode("CNSDTF-CGML")); cgVersion.appendChild(document.createTextNode("1.0")); cgUnit.appendChild(document.createTextNode("M")); cgDIM.appendChild(document.createTextNode("2")); cgTopo.appendChild(document.createTextNode("0")); cgcoordinate.appendChild(document.createTextNode("M")); cgScaleM.appendChild(document.createTextNode("")); cgDate.appendChild(document.createTextNode("2003")); cgseparator.appendChild(document.createTextNode(",")); Element CGeoMLFileTitle=document.createElement("CGeoMLFileTitle"); CGeoMLFileTitle.appendChild(cgDataMark); CGeoMLFileTitle.appendChild(cgVersion); CGeoMLFileTitle.appendChild(cgUnit); CGeoMLFileTitle.appendChild(cgDIM); CGeoMLFileTitle.appendChild(cgTopo); CGeoMLFileTitle.appendChild(cgcoordinate); CGeoMLFileTitle.appendChild(cgprojection); CGeoMLFileTitle.appendChild(cgspheriod); CGeoMLFileTitle.appendChild(cgParameters); CGeoMLFileTitle.appendChild(cgScaleM); CGeoMLFileTitle.appendChild(cgDate); CGeoMLFileTitle.appendChild(cgseparator);
return CGeoMLFileTitle; } public static void main(String[] args) { WriteGML writeGML1 = new WriteGML(); } }
另外,由于这个问题,我另外用jdom又做了个版本,能写了,就是因为jdom不支持“:”,比如想写成 <gml:Point> <gml:pos>3.14159265358979</gml:pos> </gml:Point> 这种格式的,它就提示有冒号不合法,把冒号去掉就行了。如果我想保留冒号,在jdom中有什么办法吗?这个你需要查一下:的转义符号是什么,就象大于小于号一样,不过MS的这个表中也没有,你还是想其他办法吧 Operator Description and Logical-and or Logical-or not() Negation = Equality != Not equal < * Less than <= * Less than or equal > * Greater than <= * Greater than or equal | Set operation; returns the union of two sets of nodes
首先非常感谢你,我就是还不太明白为什么在我这运行会报错,和运行环境以及设置有关吗? 我是在jbuilder7下运行的。错误是: java.lang.ClassCastException: org.apache.xerces.dom.DocumentImpl at cgeoml.WriteGML.<init>(WriteGML.java:330) at cgeoml.Application1.main(Application1.java:65)指的就是((XmlDocument)document).write(new FileOutputStream("E:\\out.txt"));这一行。可能会是哪些方面的原因会导致这样的错误?
import java.io.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.parsers.*;
import org.apache.crimson.tree.XmlDocument;/**
* <p>Title: china CGeoML</p>
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author unascribed
* @versionc 1.0
*/public class WriteGML { private Document document; public WriteGML() {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try{ DocumentBuilder builder=factory.newDocumentBuilder();
document=builder.newDocument(); }
catch(ParserConfigurationException pce){
pce.printStackTrace();
}catch(Exception e){
System.out.print(e);} Element root=document.createElement("CGeoML");
document.appendChild(root);
Node CGeoMLFileTitle=createCGeoMLFileTitle(document);
root.appendChild(CGeoMLFileTitle); try{ ((XmlDocument)document).write(new FileOutputStream("E:\\out.txt")); }
catch(Exception ioe)
{
ioe.printStackTrace();
}
} public Node createCGeoMLFileTitle(Document document)
{
Element cgDataMark=document.createElement("cgDataMark");
Element cgVersion=document.createElement("cgVersion");
Element cgUnit=document.createElement("cgUnit");
Element cgDIM=document.createElement("cgDIM");
Element cgTopo=document.createElement("cgTopo");
Element cgcoordinate=document.createElement("cgcoordinate");
Element cgprojection=document.createElement("cgprojection");
Element cgspheriod=document.createElement("cgspheriod");
Element cgParameters=document.createElement("cgParameters");
Element cgScaleM=document.createElement("cgScaleM");
Element cgDate=document.createElement("cgDate");
Element cgseparator=document.createElement("cgseparator");
cgDataMark.appendChild(document.createTextNode("CNSDTF-CGML"));
cgVersion.appendChild(document.createTextNode("1.0"));
cgUnit.appendChild(document.createTextNode("M"));
cgDIM.appendChild(document.createTextNode("2"));
cgTopo.appendChild(document.createTextNode("0"));
cgcoordinate.appendChild(document.createTextNode("M"));
cgScaleM.appendChild(document.createTextNode(""));
cgDate.appendChild(document.createTextNode("2003"));
cgseparator.appendChild(document.createTextNode(",")); Element CGeoMLFileTitle=document.createElement("CGeoMLFileTitle"); CGeoMLFileTitle.appendChild(cgDataMark);
CGeoMLFileTitle.appendChild(cgVersion);
CGeoMLFileTitle.appendChild(cgUnit);
CGeoMLFileTitle.appendChild(cgDIM);
CGeoMLFileTitle.appendChild(cgTopo);
CGeoMLFileTitle.appendChild(cgcoordinate);
CGeoMLFileTitle.appendChild(cgprojection);
CGeoMLFileTitle.appendChild(cgspheriod);
CGeoMLFileTitle.appendChild(cgParameters);
CGeoMLFileTitle.appendChild(cgScaleM);
CGeoMLFileTitle.appendChild(cgDate);
CGeoMLFileTitle.appendChild(cgseparator);
return CGeoMLFileTitle; } public static void main(String[] args) {
WriteGML writeGML1 = new WriteGML();
}
}代码太长,我截取了一段,同样说明问题,错误就是((XmlDocument)document).write(new FileOutputStream("E:\\out.xml"));这句,麻烦替我改一下,万分感激!另外,由于这个问题,我另外用jdom又做了个版本,能写了,就是因为jdom不支持“:”,比如想写成
<gml:Point>
<gml:pos>3.14159265358979</gml:pos>
</gml:Point>
这种格式的,它就提示有冒号不合法,把冒号去掉就行了。如果我想保留冒号,在jdom中有什么办法吗?
<?xml version="1.0" encoding="UTF-8"?><CGeoML>
<CGeoMLFileTitle>
<cgDataMark>CNSDTF-CGML</cgDataMark>
<cgVersion>1.0</cgVersion>
<cgUnit>M</cgUnit>
<cgDIM>2</cgDIM>
<cgTopo>0</cgTopo>
<cgcoordinate>M</cgcoordinate>
<cgprojection />
<cgspheriod />
<cgParameters />
<cgScaleM></cgScaleM>
<cgDate>2003</cgDate>
<cgseparator>,</cgseparator>
</CGeoMLFileTitle>
</CGeoML>
////////////////////////////////////////
import java.io.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.parsers.*;
import org.apache.crimson.tree.XmlDocument;/**
* <p>Title: china CGeoML</p>
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author unascribed
* @versionc 1.0
*/public class WriteGML { private Document document; public WriteGML() {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try{ DocumentBuilder builder=factory.newDocumentBuilder();
document=builder.newDocument(); }
catch(ParserConfigurationException pce){
pce.printStackTrace();
}catch(Exception e){
System.out.print(e);} Element root=document.createElement("CGeoML");
document.appendChild(root);
Node CGeoMLFileTitle=createCGeoMLFileTitle(document);
root.appendChild(CGeoMLFileTitle); try{ ((XmlDocument)document).write(new FileOutputStream("d:\\out.txt")); }
catch(Exception ioe)
{
ioe.printStackTrace();
}
} public Node createCGeoMLFileTitle(Document document)
{
Element cgDataMark=document.createElement("cgDataMark");
Element cgVersion=document.createElement("cgVersion");
Element cgUnit=document.createElement("cgUnit");
Element cgDIM=document.createElement("cgDIM");
Element cgTopo=document.createElement("cgTopo");
Element cgcoordinate=document.createElement("cgcoordinate");
Element cgprojection=document.createElement("cgprojection");
Element cgspheriod=document.createElement("cgspheriod");
Element cgParameters=document.createElement("cgParameters");
Element cgScaleM=document.createElement("cgScaleM");
Element cgDate=document.createElement("cgDate");
Element cgseparator=document.createElement("cgseparator");
cgDataMark.appendChild(document.createTextNode("CNSDTF-CGML"));
cgVersion.appendChild(document.createTextNode("1.0"));
cgUnit.appendChild(document.createTextNode("M"));
cgDIM.appendChild(document.createTextNode("2"));
cgTopo.appendChild(document.createTextNode("0"));
cgcoordinate.appendChild(document.createTextNode("M"));
cgScaleM.appendChild(document.createTextNode(""));
cgDate.appendChild(document.createTextNode("2003"));
cgseparator.appendChild(document.createTextNode(",")); Element CGeoMLFileTitle=document.createElement("CGeoMLFileTitle"); CGeoMLFileTitle.appendChild(cgDataMark);
CGeoMLFileTitle.appendChild(cgVersion);
CGeoMLFileTitle.appendChild(cgUnit);
CGeoMLFileTitle.appendChild(cgDIM);
CGeoMLFileTitle.appendChild(cgTopo);
CGeoMLFileTitle.appendChild(cgcoordinate);
CGeoMLFileTitle.appendChild(cgprojection);
CGeoMLFileTitle.appendChild(cgspheriod);
CGeoMLFileTitle.appendChild(cgParameters);
CGeoMLFileTitle.appendChild(cgScaleM);
CGeoMLFileTitle.appendChild(cgDate);
CGeoMLFileTitle.appendChild(cgseparator);
return CGeoMLFileTitle; } public static void main(String[] args) {
WriteGML writeGML1 = new WriteGML();
}
}
<gml:Point>
<gml:pos>3.14159265358979</gml:pos>
</gml:Point>
这种格式的,它就提示有冒号不合法,把冒号去掉就行了。如果我想保留冒号,在jdom中有什么办法吗?这个你需要查一下:的转义符号是什么,就象大于小于号一样,不过MS的这个表中也没有,你还是想其他办法吧
Operator Description
and Logical-and
or Logical-or
not() Negation
= Equality
!= Not equal
< * Less than
<= * Less than or equal
> * Greater than
<= * Greater than or equal
| Set operation; returns the union of two sets of nodes
我是在jbuilder7下运行的。错误是:
java.lang.ClassCastException: org.apache.xerces.dom.DocumentImpl at cgeoml.WriteGML.<init>(WriteGML.java:330) at cgeoml.Application1.main(Application1.java:65)指的就是((XmlDocument)document).write(new FileOutputStream("E:\\out.txt"));这一行。可能会是哪些方面的原因会导致这样的错误?
http://www-900.ibm.com/developerworks/cn/java/l-javaxml/index.shtml
原来是我jaxp用了1.2的版本,换成1.1的就行了。