Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JDOM中的名称空间(默认值)_Java_Xml_Jdom - Fatal编程技术网

Java JDOM中的名称空间(默认值)

Java JDOM中的名称空间(默认值),java,xml,jdom,Java,Xml,Jdom,我正在尝试使用最新的JDOM包生成XML文档。我在根元素和名称空间方面遇到问题。我需要生成这个根元素: <ManageBuildingsRequest xmlns="http://www.energystar.gov/manageBldgs/req" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.energystar.gov/manage

我正在尝试使用最新的JDOM包生成XML文档。我在根元素和名称空间方面遇到问题。我需要生成这个根元素:

<ManageBuildingsRequest 
    xmlns="http://www.energystar.gov/manageBldgs/req" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.energystar.gov/manageBldgs/req 
                        http://estar8.energystar.gov/ESES/ABS20/Schemas/ManageBuildingsRequest.xsd">
但是,ManageBuildingsRequest之后的下一个元素也具有默认名称空间,这会破坏验证:

<customer xmlns="">

有什么帮助吗?谢谢您的时间。

您用于
客户的
元素创建它时没有名称空间。您应该使用带有
命名空间的构造函数作为参数。您还可以对根元素和客户元素重用相同的
命名空间
对象

Namespace namespace = Namespace.getNamespace("http://www.energystar.gov/manageBldgs/req");
Element root = new Element("ManageBuildingsRequest", namespace);
Namespace XSI = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
root.addNamespaceDeclaration(XSI);
root.setAttribute("schemaLocation", "http://www.energystar.gov/manageBldgs/req http://estar8.energystar.gov/ESES/ABS20/Schemas/ManageBuildingsRequest.xsd", XSI);

Element customer = new Element("customer", namespace);
root.addContent(customer);
doc.addContent(root); // doc jdom Document

下面是另一种实现自定义XMLOutputProcessor的方法,该方法跳过发出空命名空间声明:

public class CustomXMLOutputProcessor extends AbstractXMLOutputProcessor {
    protected void printNamespace(Writer out, FormatStack fstack, Namespace ns)
            throws java.io.IOException {
        System.out.println("namespace is " + ns);
        if (ns == Namespace.NO_NAMESPACE) {
            System.out.println("refusing to print empty namespace");
            return;
        } else {
            super.printNamespace(out, fstack, ns);
        }
    }
}

我尝试了javanna的代码,但不幸的是它一直在文档内容中生成空名称空间。在尝试BearonTherof的代码后,XML导出得很好

创建自定义类后,必须执行以下操作:

CustomXMLOutputProcessor output = new CustomXMLOutputProcessor();
output.process(new FileWriter("/path/to/folder/generatedXML.xml"), Format.getPrettyFormat(), document);

你能发布生成xml的代码吗?你是对的,但我不太清楚为什么。我必须将名称空间传递给每个孩子,这让我非常痛苦,但这解决了它。谢谢。@jsn遇到了同样的问题,完全同意你的意见。这是一个糟糕的API。我们希望找到一个更好的解决方案。这里有一个详细的解释:这篇文章可能很旧,但对于一个有问题的API来说,这是一个相当优雅的解决方案。谢谢
CustomXMLOutputProcessor output = new CustomXMLOutputProcessor();
output.process(new FileWriter("/path/to/folder/generatedXML.xml"), Format.getPrettyFormat(), document);