Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从特定id为的XML中删除父标记下的多个标记;具体的东西“;_Java - Fatal编程技术网

Java 如何从特定id为的XML中删除父标记下的多个标记;具体的东西“;

Java 如何从特定id为的XML中删除父标记下的多个标记;具体的东西“;,java,Java,我有一个XML,看起来像这样 <TrustFrameworkPolicy xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <BuildModel> <RestSchema> <CustType Id="regular.type1">

我有一个XML,看起来像这样

<TrustFrameworkPolicy xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <BuildModel>
             <RestSchema>
                    <CustType Id="regular.type1">
                          <DataType>string</DataType>
                    </CustType>
                    <CustType Id="regular.type2">
                          <DataType>string</DataType>
                    </CustType>
                    <CustType Id="regular.Command-Nest.type1">
                          <DataType>string</DataType>
                    </CustType>
                    <CustType Id="regular.Command-Nest.type2">
                          <DataType>string</DataType>
                    </CustType>
                    <CustType Id="regular.type3">
                          <DataType>string</DataType>
                    </CustType>
                    <CustType Id="regular.Command-Nest.type4">
                          <DataType>string</DataType>
                    </CustType>
             </RestSchema>
      </BuildModel>
</TrustFrameworkPolicy>

一串
一串
一串
一串
一串
一串
我正在尝试访问ID类似于“regular.Command Nest.type1/2/3/4”的“CustType”,并将其全部删除

我用Java写了一段代码来读取下面的标记,但我不能

        Document document = getXmlAsDocument(pathToXml);

        XPath xpath = XPathFactory.newInstance().newXPath();

        XPathExpression expr = xpath.compile("//TrustFrameworkPolicy/BuildModel/RestSchema/*");
        
        Object result = expr.evaluate(document, XPathConstants.NODESET);
        
        NodeList nodes = (NodeList) result;

        for (int i = 0; i < nodes.getLength(); i++) {

            System.out.println(nodes.item(i).getNodeValue());
        }
Document Document=getXmlAsDocument(pathToXml);
XPath=XPathFactory.newInstance().newXPath();
XPathExpression expr=xpath.compile(“//TrustFrameworkPolicy/BuildModel/RestSchema/*”;
对象结果=expr.evaluate(文档,XPathConstants.NODESET);
节点列表节点=(节点列表)结果;
对于(int i=0;i
在上面的代码中,我试图打印RestSchema下的所有内容,但我无法通读它

如何以更好的方式执行此操作,我必须如上所述删除所有节点

====更新=====

第一个标签有点长

<TrustFrameworkPolicy xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns="http://schemas.microsoft.com/online/cpim/schemas/2013/06" PolicySchemaVersion="0.3.0.0" TenantId="{Settings:Tenant}" PolicyId="B2C_1A_User_MigrationClients" PublicPolicyUri="http://{Settings:Tenant}/B2C_1A_User_MigrationClients" DeploymentMode="{Settings:DeploymentMode}" UserJourneyRecorderEndpoint="urn:journeyrecorder:applicationinsights">

因此,如果用新标记替换旧标记,代码将不起作用。为什么呢


我尝试添加完整标记及其内容,但现在没有进行删除。

可以通过将XPath表达式中的
/*
替换为
/custype[以(@Id,'regular.Command Nest.type')]
开始,然后更改循环以删除找到的节点来完成。像这样:

publicstaticvoidmain(字符串[]args)引发异常{
DocumentBuilderFactory domBuilderFactory=DocumentBuilderFactory.newInstance();
domBuilderFactory.setNamespaceAware(true);
Document Document=domBuilderFactory.newDocumentBuilder().parse(新的InputSource(新的StringReader(XML));
NodeList节点=(NodeList)XPathFactory.newInstance().newXPath()
.compile(“/TrustFrameworkPolicy/BuildModel/RestSchema/custype[以(@Id,'regular.Command Nest.type')开头]”)
.evaluate(文档,XPathConstants.NODESET);
对于(int i=0;i
输出


一串
一串
一串

这有帮助吗?如果您对xpath中的名称空间有问题,不要只创建一个新问题。做一些研究,因为这样的问题以前可能已经得到了回答。我想知道为什么我们使用Transformer?@horizon来打印DOM文档。XML->DOM是通过使用
DocumentBuilder
解析XML来完成的。DOM->XML是通过使用
Transformer
生成XML来完成的。好的,我正在尝试生成一个新的XML更新文件,我可以使用Transformer将其打印到一个新文件中吗?使用上述(源XML,结果输出)?StreamResult Result=new StreamResult(new StringWriter());DOMSource=新的DOMSource(doc);变换(源、结果);字符串xmlString=result.getWriter().toString();System.out.println(xmlString)@地平线不共享。我不关心关于xpath中名称空间的重复问题。