在Java中的XML中的现有节点下添加新节点

在Java中的XML中的现有节点下添加新节点,java,xml,Java,Xml,下面是我拥有的XML <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

下面是我拥有的XML

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

                 </RestSchema>
          </BuildModel>
    </TrustFrameworkPolicy>

一串
一串
我必须在“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">
           <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>
             </RestSchema>
      </BuildModel>
</TrustFrameworkPolicy>

一串
一串
一串
一串
如何使用XPATH实现这一点。我必须在下的现有XML中创建这种类型的结构

我知道如何跟踪标记,我只需要知道如何使用XPATH在java中创建这些标记

    private static void addNewCustType(String updatedXMLPath) throws Exception {
        
        System.out.println("Adding new claim types.");
        
        Document document = getXmlAsDocument(updatedXMLPath);
        
        for (int i = 0; i < newClaim.size() ; i++ ) {
            
            // This is how I am traversing to the TAG where I need to add
            String expression = "/*[local-name() = 'TrustFrameworkPolicy']/*[local-name() = 'BuildModel']/*[local-name() = 'RestSchema']";
            
            // I need to understand how I can create a node under "above" path...
            

        }
    }

    private static Document getXmlAsDocument(String fileName) throws Exception {
    
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setNamespaceAware(true);
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(fileName);
            return doc;
        }
private static void addNewCustType(字符串updatedXMLPath)引发异常{
System.out.println(“添加新的索赔类型”);
Document Document=getXmlAsDocument(updatedXMLPath);
对于(int i=0;i
这应该可以做到。写在我的头顶上,未经测试。我已经知道它只适用于没有名称空间的情况。因此,您可以先从文档中剥离名称空间来验证它是否有效。然后再次添加它们,修改代码使其匹配

Document document = getXmlAsDocument(updatedXMLPath);
XPath xpath = XPathFactory.newInstance().newXPath();
Element restSchema = (Element)xpath.evaluate("/TrustFrameworkPolicy/BuildModel/RestSchema", document, XPathConstants.NODE);

Element dataType = document.createElement("DataType");
dataType.appendChild(document.createText("string");
Element custType = document.createElement("CustType");
custType.setAttribute("Id", "regular.Command-Nest.type1");
custType.appendChild(dataType);
restSchema.appendChild(custType);

// finally serialize using the identity transformer
对于名称空间,您必须进行两项更改:

  • 使用CreateElements而不是createElement
  • 您需要使用名称空间前缀并为xpath引擎提供名称空间上下文,而不是使用我使用的简单xpath

手动更改XML不是正确的方法。您应该创建一个类结构,并使用XMLRootElement、XMLElement等进行注释,并允许封送员完成这项工作。我已经完成了这项工作,稍后将更新我在中所做的工作。谢谢你,这可能会对社区有所帮助D