我有一个需要导航的XML文件,它类似于(完整的XML是here):

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<md:EntitiesDescriptor xmlns:md="urn:oasis:names:tc:SAML:2.0:metadata">
    <md:EntityDescriptor xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion" xmlns:saml2p="urn:oasis:names:tc:SAML:2.0:protocol" xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance" ID="_id-83bbfdd3-e4c4-42cf-a024-e4733569a4ae" entityID="https://id.eht.eu">
        <md:Organization>
            <md:OrganizationName xml:lang="it">EtnaHitech</md:OrganizationName>  
            <md:OrganizationName xml:lang="en">EtnaHitech</md:OrganizationName>   
            <md:OrganizationDisplayName xml:lang="it">EHT</md:OrganizationDisplayName>     
            <md:OrganizationDisplayName xml:lang="en">EHT</md:OrganizationDisplayName>
        </md:Organization>
    </md:EntityDescriptor>
    <md:EntityDescriptor xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion" xmlns:xs="http://www.w3.org/2001/XMLSchemainstance" ID="_gh3s48d19e23e85be40k4ab5ey331e7k4f04f73fb5" entityID="https://id.lepida.it/idp/shibboleth">
        <md:Organization>
            <md:OrganizationName xml:lang="it">Lepida</md:OrganizationName> 
            <md:OrganizationDisplayName xml:lang="it">Lepida</md:OrganizationDisplayName>   
        </md:Organization>
    </md:EntityDescriptor>
</md:EntitiesDescriptor>

假设我想要获取具有特定值的属性entityID的 node /元素md:EntityDescriptor,例如entityID="https://id.eht.eu"

我try 在Java中使用XPath,以下是我的代码:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("C:/outputTest/input.xml");

XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("//md:EntityDescriptor[@entityID='https://id.eht.eu']");
NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);

当我试着对结果骑自行车的时候:

if (nl != null && nl.getLength() != 0) {
    for (int i = 0; i < nl.getLength(); i++) {
        System.out.println(nl.item(i).getNodeValue());
    }
}

我的NodeList总是空的.我没法让这玩意儿动起来.我希望它至少在我的NodeList中获得指定的 node ,然后我将try 将其从文档中完全删除.通常,我需要创建一些东西来获取具有指定值entityID的任何 node md:EntityDescriptor,然后将其从文档中删除.

推荐答案

您的XML使用的是namespaces.使用XPath查询这类文档略有不同--XPath with namespace in Java.利用链接问题中的知识,调整代码的最简单方法是如下编辑XPath:

//*[local-name()='EntityDescriptor'][@entityID='https://id.eht.eu']

接下来,您说您想要remove(Removing nodes from an XmlDocument)个作为搜索查询结果的元素.为此,您可以一次迭代nlNode,引用其父 node ,并让它移除对该 node 的引用.调整您自己的代码,此过程可能如下所示:

for (int i = 0; i < nl.getLength(); i++) {
    Node elem = nl.item(i);
    // Debug output:
    // System.out.println(elem.getTextContent());
    elem.getParentNode().removeChild(elem);
}

最后,您可能希望store(How to update XML using XPath and Java)修改后的文档.您可以这样做:

Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.transform(new DOMSource(doc), new StreamResult(new File("C:/outputTest/output.xml")));

Java相关问答推荐

填写文本字段后锁定PDF

在spring—data中自动发现native—sql查询期间遇到重复的SQL别名[id]

';com.itextpdf.ext.html.WebColors已弃用

DTO到实体,反之亦然,控制器和服务之间的哪一层应该处理转换?

GSON期间的Java类型擦除

Spring @Value default无法计算表达式

OpenGL ES 3.0-纹理黑色

为什么我的回收视图会显示重复的列表?

FETCH类型设置为LAZY,但它仍会发送第二个请求

AWS Java SDK v2.x中没有setObjectAcl方法

为什么相同的数据条码在视觉上看起来不同?

Java Flux中的延迟增加

JavaFX复杂项目体系 struct

在权限列表中找不到我的应用程序

使用同步方法中的新线程调用同步方法

如何使用带有可选参数的类生成器?

[Guice/MissingImplementation]:未绑定任何实现

java中的网上购物车解析错误

在外部类和内部类之间,当调用外部类内部或外部的主方法时,它们的静态初始化程序的运行顺序不同

如何调查进程列表中不可见的活跃 MySQL 事务?