gpt4 book ai didi

java - 如何从 XML 文件中删除 XML 标签而不对标签名称进行硬编码

转载 作者:行者123 更新时间:2023-12-01 10:33:35 25 4
gpt4 key购买 nike

我需要使用Java删除XML文件中的所有OBJECT标签。当我直接输入父标签名称(SPAN)时,我可以删除OBJECT标签 直接将代码硬编码到源代码(“span”)中,但是我需要删除没有硬代码父标签的标签。如果我硬编码,我只能删除span标签内的对象标签。我需要删除 XML 中的所有,即使它可能位于另一个父标签内,而无需硬编码源代码中的父标签。我需要删除示例 XML 文件中 span 标记和 score 标记内可用的所有 Object 标记。对于示例 XML 文件,请查看下面的图像

Java程序

public class XmlObject {

public static void main(String[] args) {
String filePath = "/Users/myXml/Sample.xml";
File xmlFile = new File(filePath);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder;
try {
dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xmlFile);
doc.getDocumentElement().normalize();
deleteElement(doc);
doc.getDocumentElement().normalize();
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("/Users/myXml/Sample_ObjDelete.xml"));
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(source, result);
System.out.println("XML file updated successfully");

} catch (SAXException | ParserConfigurationException | IOException | TransformerException e1) {
e1.printStackTrace();
}
}

private static void deleteElement(Document doc) {
NodeList RootElement = doc.getElementsByTagName("assessmentItem");

int getRootElementLength = RootElement.getLength();

System.out.println("getRootElementLength "+getRootElementLength);

for(int k = 0; k < getRootElementLength; k++){
System.out.println("2");
Node nNode = RootElement.item(0);

Element eElement = (Element) nNode;
NodeList object = eElement.getElementsByTagName("span");
Element obj = null;


for(int i=0; i<object.getLength();i++){
obj = (Element) object.item(i);
int leng = obj.getElementsByTagName("object").getLength();
System.out.println("object:" +leng);
for(int j=0; j<leng;j++){
Node objectNode = obj.getElementsByTagName("object").item(k);
(obj).removeChild(objectNode);
}
}
}

}

}

<qualityTest>
<responseDeclaration>
<correctResponse>
<value>QualityTest</value>
</correctResponse>
</responseDeclaration>
<itemBody>
<sampleTest>
<p>Who is president of uganda?</P>
<span>
<object>
Yoweri Museveni</object>
<span>
<object>
Raúl Castro
</Object>
</span>
</sampleTest>
</itemBody>
<score>
<object>
Yingluck Shinawatra
</Object>
</score>
</qualityTest>

最佳答案

您应该递归地遍历 xml 树并删除任何对象元素的所有出现:

private static void deleteElement(Node someNode) {
NodeList childs = someNode.getChildNodes();
for (int i = 0; i < childs.getLength();) {
Node child = childs.item(i);
if (child.getNodeType() == Document.ELEMENT_NODE) {
if (child.getNodeName().equalsIgnoreCase("object")) {
child.getParentNode().removeChild(child);
continue;
} else {
deleteElement(child);
}
}
i++;
}
}

这个小代码片段将删除树中任意深度的任何名为“object”的 xml 标签。

关于java - 如何从 XML 文件中删除 XML 标签而不对标签名称进行硬编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34947930/

25 4 0