如何防止JDOM2中的XMLOutputter切割属性名称(namespace-part)?

时间:2014-11-04 10:18:07

标签: java xml jdom jdom-2

我尝试从JDOM 1.0更新到JDOM2。在JDOM 1.0中这段代码:

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
org.w3c.dom.Document doc = dbFactory.newDocumentBuilder().newDocument();
doc.setXmlVersion("1.0");

Element root = doc.createElement("Document");

root.setAttribute("xmlns", "urn:iso:foo");
root.setAttribute("xsi:schemaLocation", "urn:iso:foo bar.xsd");
root.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
doc.appendChild(root);

Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("testxml.xml"), "UTF8"));
DOMBuilder builder = new DOMBuilder();
Document jdoc = builder.build(doc);
XMLOutputter fmt = new XMLOutputter();
fmt.setFormat(Format.getPrettyFormat());
fmt.output(jdoc, out);

生成此XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:iso:foo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:iso:foo bar.xsd" />

当我使用JDOM2时,属性xsi:schemaLocation更改为schemaLocation(XML看起来像这样):

<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:iso:foo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" schemaLocation="urn:iso:foo bar.xsd" />

有没有办法让xsi:部分保留在JDOM2中?没有它,处理生成的XML的系统无法读取它(不在我的控制之下)。不确定this是否是同一个问题。

2 个答案:

答案 0 :(得分:2)

JDOM需要使用名称空间感知的DOM实现来构建JDOM文档。

我已经汇总了以下代码来说明这一点:

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.jdom2.Document;
import org.jdom2.input.DOMBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;


public class DOMvsJDOM {

    private static org.w3c.dom.Document buildDOM(String xml) throws Exception {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        dbf.setValidating(false);
        dbf.setExpandEntityReferences(false);
        DocumentBuilder db = dbf.newDocumentBuilder();
        StringReader sr = new StringReader(xml);
        InputSource is = new InputSource(sr);
        return db.parse(is);
    }

    public static void printDocument(org.w3c.dom.Document doc, OutputStream out) throws Exception {
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

        transformer.transform(new DOMSource(doc), 
             new StreamResult(new OutputStreamWriter(out, "UTF-8")));
    }

    private static void parseUsingJDOM(org.w3c.dom.Document doc) throws Exception {
//      Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("testxml.xml"), "UTF8"));
      DOMBuilder builder = new DOMBuilder();
      Document jdoc = builder.build(doc);
      XMLOutputter fmt = new XMLOutputter();
      fmt.setFormat(Format.getPrettyFormat());
      fmt.output(jdoc, System.out);
    }

    public static void main(String[] args) throws Exception {
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        org.w3c.dom.Document doc = dbFactory.newDocumentBuilder().newDocument();
        doc.setXmlVersion("1.0");

        Element root = doc.createElement("Document");

        root.setAttribute("xmlns", "urn:iso:foo");
        root.setAttribute("xsi:schemaLocation", "urn:iso:foo bar.xsd");
        root.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
        doc.appendChild(root);

        printDocument(doc, System.out);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        printDocument(doc, baos);

        System.out.println("JDOM Using captured");
        parseUsingJDOM(doc);

        String xml = new String(baos.toByteArray());
        doc = buildDOM(xml);

        System.out.println("JDOM Using parsed");
        parseUsingJDOM(doc);

    }
}

请注意,代码所做的是,手动构建DOM,输出DOM,从DOM构建JDOM,输出,然后将DOM作为字符串输出,将String重新解析为DOM,然后构建JDOM来自重新解析的XML。

这是输出(我手动在输出中添加换行符以使实际的DOM字符串在其上有自己的XML声明):

<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:iso:foo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:iso:foo bar.xsd"/>
JDOM Using captured
<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:iso:foo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" schemaLocation="urn:iso:foo bar.xsd" />
JDOM Using parsed
<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:iso:foo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:iso:foo bar.xsd" />

底线是产生输出的DOM在技术上并不是#34;名称空间感知&#34;,因此不能满足JDOM 2.0的期望。

现在,您使用以下代码设置属性:

root.setAttribute("xmlns", "urn:iso:foo");
root.setAttribute("xsi:schemaLocation", "urn:iso:foo bar.xsd");
root.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");

如果您使用了名称空间感知版本:

    root.setAttribute("xmlns", "urn:iso:foo");
    root.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
    root.setAttributeNS("http://www.w3.org/2001/XMLSchema-instance", "xsi:schemaLocation", "urn:iso:foo bar.xsd");
    doc.appendChild(root);

然后JDOM会做对。

这就是JDOM在上面解析的字符串版本中工作的原因,因为解析是以名称空间感知的方式完成的。

因此,JDOM要求在处理DOM内容时,DOM内容采用XML名称空间感知格式。这就是我的测试全部工作的原因,因为我的DOM内容都是名称空间感知的。

不幸的是,这并没有解决你遇到的实际问题....它只是解释了它。

JDOM2 应该在这个实例中与JDOM 1.x兼容,并且不兼容性是个问题。 JDOM2正在做正确的事情。事情,但它也应该做错误的&#39;事情也是如此,并且坚持为DOM上定义的那些未正确声明的属性找到命名空间。

我创建了问题138来跟踪此问题:https://github.com/hunterhacker/jdom/issues/138

答案 1 :(得分:0)

根据rolfl的提示,一种方法是不混淆DOM和JDOM内容,只使用DOM创建XML并将其保存为文件:

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
Document doc = dbFactory.newDocumentBuilder().newDocument();
doc.setXmlVersion("1.0");

Element root = doc.createElement("Document");

root.setAttribute("xmlns", "urn:iso:foo");
root.setAttribute("xsi:schemaLocation", "urn:iso:foo bar.xsd");
root.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
doc.appendChild(root);

TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformerFactory.setAttribute("indent-number", 2);
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(doc);
StreamResult xmlfile = new StreamResult(new BufferedWriter(new OutputStreamWriter(new FileOutputStream("testxml.xml"), "UTF8")));
transformer.transform(source, xmlfile);

但这不是一个完全有效的答案,因为问题是如何使用JDOM2来完成任务。