当前位置: 首页 > 知识库问答 >
问题:

从XML文件获取命名空间

羿昊英
2023-03-14

我有这个XML文件,我需要知道名称空间的URI。

<?xml version="1.0" encoding="utf-8"?>
<fe:Facturae 
    xmlns:ds="http://www.w3.org/2000/09/xmldsig#" 
    xmlns:fe="http://www.facturae.es/Facturae/2014/v3.2.1/Facturae" >
    <FileHeader>
    </FileHeader>
</fe:Facturae>

我使用Java(jdk16)和这段代码来获取它们:

        try {
            
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setNamespaceAware(true);
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc1 = builder.parse(new File(FACTURA_MODELO_XML));
            Element element = doc1.getDocumentElement();
            
            logger.info("element         : {}", element.getNodeName());
            
            NamedNodeMap attributes = element.getAttributes();
            
            if (attributes != null){
                for (int i = 0; i < attributes.getLength(); i++){
                    Attr attribute = (Attr)attributes.item(i);
                    logger.info("attribute.Name         : {}", attribute.getNodeName());
                    logger.info("attribute.NamespaceURI : {}", attribute.getNamespaceURI());
                    logger.info("attribute.Prefix       : {}", attribute.getPrefix());
          
                }
            }
         } catch (Exception ex){
            // print exception
         }

但是运行这段代码会给我带来以下结果:


[main] INFO attribute.Name         : xmlns:ds
[main] INFO attribute.NamespaceURI : http://www.w3.org/2000/xmlns/
[main] INFO attribute.Prefix       : xmlns
[main] INFO attribute.Name         : xmlns:fe
[main] INFO attribute.NamespaceURI : http://www.w3.org/2000/xmlns/
[main] INFO attribute.Prefix       : xmlns

属性的名称是正确的,但是NamespaceURI和前缀不是我要寻找的值。

我到底做错了什么?

提前感谢您的回答。

共有1个答案

唐俊英
2023-03-14

您不需要属性的命名空间,而是需要属性值。

代替

logger.info("attribute.NamespaceURI : {}", attribute.getNamespaceURI());

你应该用

logger.info("attribute.Value : {}", attribute.getValue());

然后输出为:

element         : {}fe:Facturae
attribute.Name         : {}xmlns:ds
attribute.Value        : {}http://www.w3.org/2000/09/xmldsig#
attribute.Prefix       : {}xmlns
attribute.Name         : {}xmlns:fe
attribute.Value        : {}http://www.facturae.es/Facturae/2014/v3.2.1/Facturae
attribute.Prefix       : {}xmlns
 类似资料:
  • 主要内容:命名冲突,使用前缀来避免命名冲突,XML 命名空间 - xmlns 属性,统一资源标识符(URI,全称 Uniform Resource Identifier),默认的命名空间,实际使用中的命名空间XML 命名空间提供避免元素命名冲突的方法。 命名冲突 在 XML 中,元素名称是由开发者定义的,当两个不同的文档使用相同的元素名时,就会发生命名冲突。 这个 XML 携带 HTML 表格的信息: <table> <tr> <td>Apples</td> <td>Bananas</td> <

  • 我谷歌了很多,但运气不好。我不能从XML列中检索数据,这些数据来自使用sp_OAGetProperty的Web服务。 XML列包含。。 当我执行下面的语句时,我得到了NULL (但如果我删除所有XML名称空间xmlns:xsi=”就可以了http://www.w3.org/2001/XMLSchema-instance“xmlns:xsd=”http://www.w3.org/2001/XMLSc

  • 问题内容: 有没有办法从或从中获取文件名? 问题答案: 看起来答案是否定的: http://download.oracle.com/javase/1.4.2/docs/api/java/io/FileOutputStream.html http://docs.oracle.com/javase/7/docs/api/index.html?java/io/FileOutputStream.html

  • 问题内容: 如果我有文件指针,是否可以获取文件名? 可以使用吗? 问题答案: 您可以通过获得路径。例: 如果 只 需要文件名,则可能需要: 文件对象文档(适用于Python 2)在此处。

  • XML信封命名空间的正确URI是什么。我见过很多,例如。”http://www.w3.org/2001/12/soap-envelope“,”http://www.w3.org/2001/06/soap-envelope“,”http://www.w3.org/2003/05/soap-envelope“,”http://schemas.xmlsoap.org/soap/envelope/“。请告

  • 我在Azure-Databricks中使用spark-xml库。但是我不能得到正确的选项来读取这种包含多个名称空间的文件。 因此,我正在寻找一些帮助,让这在选项编码,或任何其他方法。 这是一个剥离的样品。