从Java中的元素中获取命名空间(使用DOM) [英] Retrieving Namespaces from Element in Java (using DOM)

查看:174
本文介绍了从Java中的元素中获取命名空间(使用DOM)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下XML示例:

 <?xml version =1.0encoding =UTF-8 ?> 
< OpenSearchDescription xmlns =http://a9.com/-/spec/opensearch/1.1/
xmlns:custom =http://example.com/opensearchextensions/1.0/> ;
< ShortName>测试< / ShortName>
<描述>测试....< / Description>
< Url template =whatevertype =what/>
<查询自定义:color =bluerole =example/>
< / OpenSearchDescription>

我关心的是查询元素。它有一个Namespace属性,而在java中,您将需要一个 namespaceURI 来检索该值。



我的问题是:如何从根元素(在这种情况下, OpenSearchDescription 元素)中检索命名空间列表?我想要在查询上可以使用的属性,前缀和命名空间URI。



谢谢。 p>

PS:我在java中使用DOM,在JSE中是标准的。如果有可能,我愿意搬到XPath。要求是只需要使用Java Standard API。

解决方案

这可能让你开始, factory.setNamespaceAware(true)是获取命名空间数据的关键。

  public static void main(String [] args)throws ParserConfigurationException,SAXException,
IOException
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
文档doc = builder.parse(new File(args [0]));
元素root = doc.getDocumentElement();
//打印根名空间
printAttributesInfo((Node)root);

NodeList childNodes = root.getChildNodes(); (int i = 0; i< childNodes.getLength(); i ++)
{
printAttributesInfo(childNodes.item(i));

}
}

public static void printAttributesInfo(Node root)
{
NamedNodeMap attributes = root.getAttributes();
if(attributes!= null)
{
for(int i = 0; i< attributes.getLength(); i ++)
{
Node node = attributes.item(ⅰ);
if(node.getNodeType()== Node.ATTRIBUTE_NODE)
{
String name = node.getNodeName();
System.out.println(name ++ node.getNamespaceURI());
}
}
}
}


I have the following XML example:

<?xml version="1.0" encoding="UTF-8"?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/"
    xmlns:custom="http://example.com/opensearchextensions/1.0/">
    <ShortName>Test</ShortName>
    <Description>Testing....</Description>
    <Url template="whatever" type="what" />
    <Query custom:color="blue" role="example" />
</OpenSearchDescription>

My concern is with Query element. It has a Namespace attribute and, in java, you will need a namespaceURI to retrieve the value.

My question is: How would I retrieve the list of namespaces from the root element (in this case, OpenSearchDescription element)? I want the attribute, prefix and namespace URI that I can use to request on Query.

Thanks.

PS: I'm using DOM in java, standard in JSE. I'm willing to move to XPath, if it's possible. The requirement is that only the Java Standard API needs to be used.

解决方案

This might get you started, factory.setNamespaceAware(true) is the key for getting the namespace data after all.

public static void main(String[] args) throws ParserConfigurationException, SAXException,
        IOException
{
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(new File(args[0]));
    Element root = doc.getDocumentElement();
    //prints root name space
    printAttributesInfo((Node) root);

    NodeList childNodes = root.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++)
    {
        printAttributesInfo(childNodes.item(i));
    }
}

public static void printAttributesInfo(Node root)
{
    NamedNodeMap attributes = root.getAttributes();
    if (attributes != null)
    {
        for (int i = 0; i < attributes.getLength(); i++)
        {
            Node node = attributes.item(i);
            if (node.getNodeType() == Node.ATTRIBUTE_NODE)
            {
                String name = node.getNodeName();
                System.out.println(name + " " + node.getNamespaceURI());
            }
        }
    }
}

这篇关于从Java中的元素中获取命名空间(使用DOM)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆