【问题标题】:XML not getting parsed due to XML metadata tag [duplicate]由于 XML 元数据标签,XML 未得到解析 [重复]
【发布时间】:2021-04-15 06:44:14
【问题描述】:

我正在尝试解析 XML 并到达一个节点并删除正在使用以下代码...

    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));

下面是我正在解析的 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 标签)

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

有人可以告诉我发生了什么并指导我如何解决这个问题...或分享一个我可以参考的链接。

【问题讨论】:

  • 你能添加你在解析时遇到的错误吗?
  • 修改对洞察的益处有限。您在原始 XML 上使用了哪些格式正确/有效性检查器?
  • 你好@M.Deinum 我没有收到任何错误,我想做的是....获取我拥有的XML,删除所有具有字符串“id =常规的.Command-Nest。”在我的本地计算机上的新文件中打印带有已删除标签的新更新 XML。因此,当我运行我的代码时,(使用更新的 )我得到了正确的 XML 输出,其中删除了数据......但是原来的 很长......我得到与输出相同的文件,没有任何改变
  • @BhargavPatel 我刚刚删除了 "xmlns="schemas.microsoft.com/online/cpim/schemas/2013/06"" 并且它起作用了,为什么会这样?
  • 您删除了包含该标记的根命名空间。这样,您的 xpath 表达式将不匹配(因为它在命名空间中),删除它匹配,因为没有命名空间。要解析第一个,您需要使用正确的命名空间设置 XPath 以使其匹配。

标签: java xml xsd


【解决方案1】:

您不能忽略命名空间,它们是基础。如果您的 XML 元素位于命名空间中,则需要在 XPath 表达式中考虑这一点。

请搜索“XPath 默认命名空间”。有超过 1800 个结果,显示这是一个普遍的问题。阅读一些高分答案以了解问题。

【讨论】:

    猜你喜欢
    • 2012-03-31
    • 1970-01-01
    • 1970-01-01
    • 2020-11-30
    • 2021-02-28
    • 2012-01-24
    • 2013-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多