通过jdom创建具有名称空间的xml文件时出现DomException

时间:2018-10-03 06:16:32

标签: java xml jdom

我想创建一个看起来像这样的xml文件:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<gpx xmlns="http://www.topografix.com/GPX/1/1" version="1.1" creator="Wikipedia"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">

在使用setAttributeNS创建并执行此操作时,我得到一个DomException,该错误说我想创建一个关于名称空间错误的文件。

代码如下:

try{
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        DOMImplementation di = db.getDOMImplementation();
        Document doc = di.createDocument("http://www.topografix.com/GPX/1/1", "gpx", null);
        doc.setXmlVersion("1.0");
        Element rootgpx = doc.getDocumentElement();
        rootgpx.setAttributeNS("http://www.topografix.com/GPX/1/1", "xmlns", "http://www.topografix.com/GPX/1/1");
        rootgpx.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance" );
        rootgpx.setAttributeNS("http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd", "xsi:schemaLocation", "http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd");
        doc.appendChild(rootgpx);

我显然做错了什么,但我不知道该怎么办。 有人可以帮忙吗?

0 个答案:

没有答案