![](/img/trans.png)
[英]How can I show the values of Nodes with specific id and tag name and under specific parent node in Java, XML file?
[英]How to delete a multiple tags under a parent tag from XML with specific id ="something specific"
我有一個看起來像這樣的 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 為 LIKE "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());
}
在上面的代碼中,我試圖打印 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 表達式中將/*
替換為/CustType[starts-with(@Id, 'regular.Command-Nest.type')]
來完成,然后更改循環以刪除找到的節點。 像這樣:
public static void main(String[] args) throws Exception {
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));
}
static final String XML =
"<TrustFrameworkPolicy xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\r\n" +
" <BuildModel>\r\n" +
" <RestSchema>\r\n" +
" <CustType Id=\"regular.type1\">\r\n" +
" <DataType>string</DataType>\r\n" +
" </CustType>\r\n" +
" <CustType Id=\"regular.type2\">\r\n" +
" <DataType>string</DataType>\r\n" +
" </CustType>\r\n" +
" <CustType Id=\"regular.Command-Nest.type1\">\r\n" +
" <DataType>string</DataType>\r\n" +
" </CustType>\r\n" +
" <CustType Id=\"regular.Command-Nest.type2\">\r\n" +
" <DataType>string</DataType>\r\n" +
" </CustType>\r\n" +
" <CustType Id=\"regular.type3\">\r\n" +
" <DataType>string</DataType>\r\n" +
" </CustType>\r\n" +
" <CustType Id=\"regular.Command-Nest.type4\">\r\n" +
" <DataType>string</DataType>\r\n" +
" </CustType>\r\n" +
" </RestSchema>\r\n" +
" </BuildModel>\r\n" +
"</TrustFrameworkPolicy>";
Output
<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.type3">
<DataType>string</DataType>
</CustType>
</RestSchema>
</BuildModel>
</TrustFrameworkPolicy>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.