Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.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 由于XML元数据标记,无法解析XML_Java_Xml_Xsd - Fatal编程技术网

Java 由于XML元数据标记,无法解析XML

Java 由于XML元数据标记,无法解析XML,java,xml,xsd,Java,Xml,Xsd,我正在尝试解析XML并访问一个节点,delete正在使用下面的代码 DocumentBuilderFactory domBuilderFactory = DocumentBuilderFactory.newInstance(); domBuilderFactory.setNamespaceAware(true); Document document = domBuilderFactory.newDocumentBuilder().parse(new InputSource

我正在尝试解析XML并访问一个节点,delete正在使用下面的代码

    DocumentBuilderFactory domBuilderFactory = DocumentBuilderFactory.newInstance();
    domBuilderFactory.setNamespaceAware(true);
    Document document = domBuilderFactory.newDocumentBuilder().parse(new InputSource(new StringReader(XML)));
    
    NodeList nodes = (NodeList) XPathFactory.newInstance().newXPath()
            .compile("/TrustFrameworkPolicy/BuildModel/RestSchema/CustType[starts-with(@Id, 'regular.Command-Nest.type')]")
            .evaluate(document, XPathConstants.NODESET);
    for (int i = 0; i < nodes.getLength(); i++) {
        nodes.item(i).getParentNode().removeChild(nodes.item(i));
    }

    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.transform(new DOMSource(document), new StreamResult(System.out));
DocumentBuilderFactory域builderFactory=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
下面是我正在解析的XML…但问题是,由于 TrustFrameworkPolicy因为它很长而且有很多数据

<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">
              <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>

一串
一串
一串
一串
一串
一串
但是当我通过修改XML来测试我的java代码是否正确时,我正在编辑TrustFrameworkPolicy就像这样…它工作正常,我能够解析并完成我的工作

<TrustFrameworkPolicy xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <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>

一串
一串
一串
一串
一串
一串
输出(带有更新的TrustFrameworkPolicy标记)


电子资讯B2cint.onmicrosoft.com
B2C_1A_用户_信任框架扩展_多租户
一串
一串

有人能告诉我发生了什么事,并指导我如何度过这一难关……或者分享一个我可以参考的链接。

你不能忽视名称空间,它们是最基本的。如果XML元素位于命名空间中,则需要在XPath表达式中说明这一点


请搜索“XPath默认名称空间”。有超过1800个结果,表明这是一个多么普遍的问题。阅读一些得分较高的答案以了解问题。

您能否添加纵火时出现的错误?修改对洞察的好处有限。您在原始XML上尝试了哪些格式良好/有效性检查?Hello@M.Deinum我没有收到任何错误,我正在尝试做的是…使用我拥有的XML,删除所有包含字符串“id=regular.Command Nest”的XML。打印新更新的XML,并删除标记,将其保存在本地计算机上的新文件中。所以当我运行我的代码时,(更新后的)我得到了正确的XML输出,数据被删除了…但是使用长的原始文件…我得到了与输出相同的文件,没有任何内容changed@BhargavPatel我刚刚删除了“xmlns=”,它成功了,为什么会发生这种情况?您删除了包含标记的根命名空间。这样一来,xpath表达式就不匹配了(因为它在一个名称空间中),因为没有名称空间,所以就删除了它匹配的表达式。要解析第一个,您需要使用适当的名称空间设置XPath以使其匹配。
<TrustFrameworkPolicy xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <BasePolicy>
    <TenantId>egenginfob2cint.onmicrosoft.com</TenantId>
    <PolicyId>B2C_1A_User_TrustFrameworkExtensions_MultiTenant</PolicyId>
  </BasePolicy>
  
  <BuildingBlocks>
    <ClaimsSchema>
      <ClaimType Id="custom.rule1">
        <DataType>string</DataType>
      </ClaimType>
      <ClaimType Id="custom.rule2">
        <DataType>string</DataType>
      </ClaimType>
      
      
      
      
    </ClaimsSchema>
  </BuildingBlocks>
</TrustFrameworkPolicy>