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

时间:2016-01-22 13:33:24

标签: java jquery xml

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

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> 

3 个答案:

答案 0 :(得分:1)

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

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++;
    }
}

这个小代码片段将删除任何名为&#34; object&#34;的xml-tag。在树的任何深度。

答案 1 :(得分:0)

您可以使用Xpath和XpathExpression来 Span 得分标记

  XPath xPath =  XPathFactory.newInstance().newXPath();
String expression = "//span";           
NodeList spanNodeList = (NodeList) xPath.compile(expression).evaluate(document, XPathConstants.NODESET);

spanNodeList将为您提供所有span节点。所以你迭代spanNodeList并删除span元素,如下面的代码

for (int i = 0; i < spanNodeList .getLength(); i++) {
            Node spanItem= spanNodeList .item(i);

            Node parentNode = spanItem.getParentNode();
            parentNode.removeChild(spanItem);
}

得分标记

也是如此

答案 2 :(得分:0)

您可以使用XPath选择除display: none元素之外的所有元素。例如,您可以将其放在名为<object>的文件中:

strip-object.xsl

注意第一个模板规则,它对<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="xml" omit-xml-declaration="yes"/> <xsl:template match="//object"/> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> </xsl:stylesheet> 个节点没有任何作用,有效地丢弃它们。第二个模板规则对所有其他节点生效,将它们完全复制。

要使用它,请使用.xsl文件初始化Transformer:

object

如果您只想删除属于Transformer transformer = transformerFactory.newTransformer( new StreamSource(new File("/Users/myXml/strip-object.xsl"))); object元素的span个元素,则可以更改XPath表达式:

score