从GWT代码调用getElementsByTagNameNS

问题描述 投票:1回答:2

我有一个com.google.gwt.xml.client.Document对象形式的XML文档。由于Document类上没有提供“getElementsByTagNameNS”API,是否可以使用一些本机Javascript函数来实现相同的功能?

API应该采用两个参数1 - 名称空间前缀。 2 - XML标记名称。

我需要这个,因为如果XML标记具有名称空间前缀,则最近的Chrome更新(v60)不会返回Document.getElementsByTagName(“book”)的NodeList。

javascript xml gwt
2个回答
0
投票

c.g.g.xml从不支持名称空间,因此不适用于使用XML名称空间的文档。它还包装本机对象,并且不提供对它们的任何访问。

如今,我认为API的浏览器支持几乎没有区别(例如IE6的情况并非如此),所以你最好重写你的代码以使用JsInterop而不是cggxml(看看元素2,应该已经有了“映射”)


0
投票

我已经实现了getElementsByTagName函数的快速而又脏的替代方案。它采用完全限定的标记名称并返回具有确切标记名称的所有后代元素节点。

/**
 * Builds a list of the descendant elements with the supplied tag name from the {@code parent} node. The result
 * nodes are ordered by a depth-first traversal of the element graph.
 * 
 * @param tag   {@link String} = The tag we are looking for. Fully qualified with the namespace if present (e.g. {@code hh:label})
 * @param parent    {@link Node} = Root node of the search
 * @return {@link List<Node>} = The node list
 */
public static List<Node> getElementsByTagName(String tag, Node parent) {
    List<Node> result = new ArrayList<>();

    if (parent == null) {
        return result;
    }

    NodeList children = parent.getChildNodes();
    for (int childIdx = 0; childIdx < children.getLength(); childIdx++) {
        Node child = children.item(childIdx);
        if (child.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }
        if (((Element) child).getTagName() == tag) {
            result.add(child);
        }
        getElementsByTagName(tag, child, result);
    }
    return result;
}

private static void getElementsByTagName(String tag, Node parent, List<Node> partialResult) {
    if (parent == null) {
        return;
    }

    NodeList children = parent.getChildNodes();
    for (int childIdx = 0; childIdx < children.getLength(); childIdx++) {
        Node child = children.item(childIdx);
        if (child.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }
        if (((Element) child).getTagName() == tag) {
            partialResult.add(child);
        }
        getElementsByTagName(tag, child, partialResult);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.