Java+DOM:如何设置(已创建)文档的基本命名空间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1492428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:24:25  来源:igfitidea点击:

Java+DOM: How do I set the base namespace of an (already created) Document?

javaxmldomnamespacesdocument

提问by ivan_ivanovich_ivanoff

I am dealing with an already createdDocument object. I have to be able to set it's base namespace (attribute name "xmlns") to certain value. My input is DOM and is something like:

我正在处理一个已经创建的Document 对象。我必须能够将它的基本命名空间(属性名称“xmlns”)设置为某个值。我的输入是 DOM,类似于:

<root>...some content...</root>

What I need is DOM which is something like:

我需要的是 DOM,它类似于:

<root xmlns="myNamespace">...some content...</root>

That's it. Easy, isn't it? Wrong! Not with DOM!

就是这样。很简单,不是吗?错误的!不是 DOM!

I have tried the following:

我尝试了以下方法:

1) Using doc.getDocumentElement().setAttribute("xmlns","myNamespace")

1) 使用 doc.getDocumentElement().setAttribute("xmlns","myNamespace")

I get a document with empty xmlns (it works on anyother attribute name!)

我得到一个带有空 xmlns 的文档(它适用于任何其他属性名称!)

<root xmlns="">...</root>

2) Using renameNode(...)

2) 使用 renameNode(...)

First clonethe document:

首先克隆文档:

Document input = /*that external Document whose namespace I want to alter*/;

DocumentBuilderFactory BUILDER_FACTORY_NS = DocumentBuilderFactory.newInstance();
BUILDER_FACTORY_NS.setNamespaceAware(true);
Document output = BUILDER_NS.newDocument();
output.appendChild(output.importNode(input.getDocumentElement(), true));

I'm really missing document.clone(), but perhaps it's just me.

我真的很想念 document.clone(),但也许只有我一个人。

Now rename the root node:

现在重命名根节点

output.renameNode(output.getDocumentElement(),"myNamespace",
    output.getDocumentElement().getTagName());

Now isn't thatstraightforward? ;)

现在不是那么简单吗?;)

What I get now is:

我现在得到的是:

<root xmlns="myNamespace">
    <someElement xmlns=""/>
    <someOtherElement xmlns=""/>
</root>

So (as all of us have expected, right?), this renames the namespace only of the the root node.

所以(正如我们所有人所期望的那样,对吧?),这重命名了根节点的命名空间。

Curse you, DOM!

诅咒你,DOM!

Is there any way to do this recursively (without writing an own recursive method)?

有没有办法递归地做到这一点(不编写自己的递归方法)?

Please help ;)

请帮忙 ;)

Please don't advice me to do some fancy workaround, such as transforming DOM to something else, alter the namespace there, and transform it back. I need DOM because it's the fastest standard way to manipulate XML.

请不要建议我做一些花哨的解决方法,例如将 DOM 转换为其他东西,更改那里的命名空间,然后将其转换回来。我需要 DOM,因为它是操作 XML 的最快标准方法。

Note: I'm using the latest JDK.

注意:我使用的是最新的 JDK。

EDIT
Removed wrong assumptions from the question, which had to do with namespace prefix.

编辑
从问题中删除了错误的假设,这与命名空间前缀有关

回答by Aaron Digulla

In addition to setting the prefix, you must also declareyour namespace somewhere.

除了设置前缀之外,您还必须在某处声明您的命名空间。

[EDIT] If you look into the package org.w3c.dom, you'll notice that there is no support for namespaces whatsoever except that you can create a Document node with a namespace URI:

[编辑] 如果您查看 package org.w3c.dom,您会注意到除了您可以创建具有名称空间 URI 的 Document 节点之外,不支持任何名称空间:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
DOMImplementation DOMImplementation = builder.getDOMImplementation();
Document doc = DOMImplementation.createDocument(
    "http://www.somecompany.com/2005/xyz", // namespace
    "root",
    null /*DocumentType*/);

Element root = doc.getDocumentElement();
root.setPrefix("xyz");
root.setAttribute(
    "xmlns:xyz",
    "http://www.somecompany.com/2005/xyz");

With the standard W3C DOM API of Java 5 (and up), it's not possible to modifythe namespace of a node.

使用 Java 5(及更高版本)的标准 W3C DOM API,无法修改节点的命名空间。

But the W3C DOM API is just a couple of interfaces. So what you should try is to look at the implementation (i.e. the actual class of your document instance), cast it to the real type. This type should have additional methods and if you're lucky, you can use those to modify the namespace.

但是 W3C DOM API 只是几个接口。因此,您应该尝试查看实现(即文档实例的实际类),将其转换为真实类型。这种类型应该有额外的方法,如果幸运的话,你可以使用这些方法来修改命名空间。

回答by ivan_ivanovich_ivanoff

Well, here goes the recursive "solution":
(I still hope that someone might find a better way to do this)

好吧,这里是递归“解决方案”:(
我仍然希望有人能找到更好的方法来做到这一点)

public static void renameNamespaceRecursive(Document doc, Node node,
        String namespace) {

    if (node.getNodeType() == Node.ELEMENT_NODE) {
        System.out.println("renaming type: " + node.getClass()
            + ", name: " + node.getNodeName());
        doc.renameNode(node, namespace, node.getNodeName());
    }

    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); ++i) {
        renameNamespaceRecursive(doc, list.item(i), namespace);
    }
}

Seems to work, although I don't know if it's correct to rename only the node type ELEMENT_NODE, or if other node types must be renamed.

似乎有效,尽管我不知道仅重命名节点类型 ELEMENT_NODE 是否正确,或者是否必须重命名其他节点类型。

回答by Jorge Ferreira

I had the very same problem today. I ended up using parts of @ivan_ivanovich_ivanoff answerbut removed the recursion and fixed some bugs.

我今天遇到了同样的问题。我最终使用了@ivan_ivanovich_ivanoff 答案的一部分,但删除了递归并修复了一些错误。

Very important:if old namespace is nullyou must add two translations, one from nullto your new namespaceURIand another from ""to your new namespaceURI. This happens because the first call to renameNodewill change existing nodes that have a nullnamespaceURIto xmlns="".

非常重要:如果旧命名空间是null你必须添加两个翻译,一个 fromnull到你的 new namespaceURI,另一个 from""到你的 new namespaceURI。发生这种情况是因为第一次调用 torenameNode将更改具有nullnamespaceURIto 的现有节点xmlns=""

Example of usage:

用法示例:

Document xmlDoc = ...;

new XmlNamespaceTranslator()
    .addTranslation(null, "new_ns")
    .addTranslation("", "new_ns")
    .translateNamespaces(xmlDoc);

// xmlDoc will have nodes with namespace null or "" changed to "new_ns"

Full source code follows:

完整源代码如下:

public  class XmlNamespaceTranslator {

    private Map<Key<String>, Value<String>> translations = new HashMap<Key<String>, Value<String>>();

    public XmlNamespaceTranslator addTranslation(String fromNamespaceURI, String toNamespaceURI) {
        Key<String> key = new Key<String>(fromNamespaceURI);
        Value<String> value = new Value<String>(toNamespaceURI);

        this.translations.put(key, value);

        return this;
    }

    public void translateNamespaces(Document xmlDoc) {
        Stack<Node> nodes = new Stack<Node>();
        nodes.push(xmlDoc.getDocumentElement());

        while (!nodes.isEmpty()) {
            Node node = nodes.pop();
            switch (node.getNodeType()) {
            case Node.ATTRIBUTE_NODE:
            case Node.ELEMENT_NODE:
                Value<String> value = this.translations.get(new Key<String>(node.getNamespaceURI()));
                if (value != null) {
                    // the reassignment to node is very important. as per javadoc renameNode will
                    // try to modify node (first parameter) in place. If that is not possible it
                    // will replace that node for a new created one and return it to the caller.
                    // if we did not reassign node we will get no childs in the loop below.
                    node = xmlDoc.renameNode(node, value.getValue(), node.getNodeName());
                }
                break;
            }

            // for attributes of this node
            NamedNodeMap attributes = node.getAttributes();
            if (!(attributes == null || attributes.getLength() == 0)) {
                for (int i = 0, count = attributes.getLength(); i < count; ++i) {
                    Node attribute = attributes.item(i);
                    if (attribute != null) {
                        nodes.push(attribute);
                    }
                }
            }

            // for child nodes of this node
            NodeList childNodes = node.getChildNodes();
            if (!(childNodes == null || childNodes.getLength() == 0)) {
                for (int i = 0, count = childNodes.getLength(); i < count; ++i) {
                    Node childNode = childNodes.item(i);
                    if (childNode != null) {
                        nodes.push(childNode);
                    }
                }
            }
        }
    }

    // these will allow null values to be stored on a map so that we can distinguish
    // from values being on the map or not. map implementation returns null if the there
    // is no map element with a given key. If the value is null there is no way to
    // distinguish from value not being on the map or value being null. these classes
    // remove ambiguity.
    private static class Holder<T> {

        protected final T value;

        public Holder(T value) {
            this.value = value;
        }

        public T getValue() {
            return value;
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((value == null) ? 0 : value.hashCode());
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            Holder<?> other = (Holder<?>) obj;
            if (value == null) {
                if (other.value != null)
                    return false;
            } else if (!value.equals(other.value))
                return false;
            return true;
        }

    }

    private static class Key<T> extends Holder<T> {

        public Key(T value) {
            super(value);
        }

    }

    private static class Value<T> extends Holder<T> {

        public Value(T value) {
            super(value);
        }

    }
}

回答by jawahar prabhu

we can change the xml namespace using sax parser, try this

我们可以使用 sax 解析器更改 xml 命名空间,试试这个

import java.util.ListIterator;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Namespace;
import org.dom4j.QName;
import org.dom4j.Visitor;
import org.dom4j.VisitorSupport;
import org.dom4j.io.SAXReader;

public class VisitorExample {

  public static void main(String[] args) throws Exception {
    Document doc = new SAXReader().read("test.xml");
    Namespace oldNs = Namespace.get("oldNamespace");
    Namespace newNs = Namespace.get("newPrefix", "newNamespace");
    Visitor visitor = new NamespaceChangingVisitor(oldNs, newNs);
    doc.accept(visitor);
    System.out.println(doc.asXML());
  }
}

class NamespaceChangingVisitor extends VisitorSupport {
  private Namespace from;
  private Namespace to;

  public NamespaceChangingVisitor(Namespace from, Namespace to) {
    this.from = from;
    this.to = to;
  }

  public void visit(Element node) {
    Namespace ns = node.getNamespace();

    if (ns.getURI().equals(from.getURI())) {
      QName newQName = new QName(node.getName(), to);
      node.setQName(newQName);
    }

    ListIterator namespaces = node.additionalNamespaces().listIterator();
    while (namespaces.hasNext()) {
      Namespace additionalNamespace = (Namespace) namespaces.next();
      if (additionalNamespace.getURI().equals(from.getURI())) {
        namespaces.remove();
      }
    }
  }

}

回答by T-and-M Mike

A slight variation of Ivan's original post worked for me: setting the attribute on the document node.

Ivan 的原始帖子的轻微变化对我有用:在文档节点上设置属性。

xslRoot.setAttribute("xmlns:fo", "http://www.w3.org/1999/XSL/Format");

where

在哪里

  • xslRootis the document/root element/node,
  • fois the namespace ID
  • xslRoot是文档/根元素/节点,
  • fo是命名空间 ID

Hope that helps someone!

希望对某人有所帮助!

Mike Watts

迈克·沃茨

回答by Kevin Krouse

If you are ok with using the Xerces classes, you can create a DOMParser that replaces the URI of attributes and elements with your fixed up URIs:

如果您可以使用 Xerces 类,您可以创建一个 DOMParser,用您固定的 URI 替换属性和元素的 URI:

import org.apache.xerces.parsers.DOMParser;

public static class MyDOMParser extends DOMParser {
    private Map<String, String> fixupMap = ...;

    @Override
    protected Attr createAttrNode(QName attrQName)
    {
        if (fixupMap.containsKey(attrQName.uri))
            attrQName.uri = fixupMap.get(attrQName.uri);
        return super.createAttrNode(attrQName);
    }

    @Override
    protected Element createElementNode(QName qName)
    {
        if (fixupMap.containsKey(qName.uri))
            qName.uri = fixupMap.get(qName.uri);
        return super.createElementNode(qName);
    }       
}

The elsewhere, you can parse the

在别处,你可以解析

DOMParse p = new MyDOMParser(...);
p.parse(new InputSource(inputStream));
Document doc = p.getDocument();

回答by OcK

Let's say you've got your Document instance..

假设你有你的 Document 实例..

import org.dom4j.*;

{

    static final String         YOUR_NAMESPACE_PREFIX =   "PREFIX"; 
    static final String         YOUR_NAMESPACE_URI    =   "URI"; 

    Document document = ...

    //now get the root element
    Element element = document.getRootElement();
    renameNamespaceRecursive(element);
    ...

    //End of this method
}

//the recursive method for the operation
void renameNamespaceRecursive(Element element) {
    element.setQName(new QName(element.getName(), DocumentHelper.createNamespace(YOUR_NAMESPACE_PREFIX, YOUR_NAMESPACE_URI)));
    for (Iterator i  = element.elementIterator(); i.hasNext();) {
        renameNamespaceRecursive((Element)i.next());
    }
}

That should do.

那应该做。

回答by cobus

I solved using org.jdom.Element:

我使用 org.jdom.Element 解决了:

Java:

爪哇:

import org.jdom.Element;
...
Element kml = new Element("kml", "http://www.opengis.net/kml/2.2");

XML:

XML:

<kml xmlns="http://www.opengis.net/kml/2.2">; 
...
</kml>