简体   繁体   中英

Remove all empty nodes from XML with Groovy

I want to remove all empty nodes by using groovy

This is xml and I want to remove telephone and team

<Profile>
    <Name>Fin</Name>
    <Aga>20</Aga>
    <Contact>
        <Mobile>1819</Mobile>
        <Telephone/>
    </Contact>
    <Team>
        <Team1/>
        <Team2/>
    </Team>
</Profile>

This is my code.

  import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.DocumentBuilderFactory;
  import javax.xml.transform.OutputKeys;
  import javax.xml.transform.Transformer;
  import javax.xml.transform.TransformerException;
  import javax.xml.transform.TransformerFactory;
  import javax.xml.transform.dom.DOMSource;
  import javax.xml.transform.stream.StreamResult;

   import org.w3c.dom.Document;
   import org.w3c.dom.Node;


   import groovy.xml.*

boolean cleanNode( def node ) {
    node.value().with { a ->
        a.findAll { !it.value }.each { a.remove( it.key ) }
    }
    node.children().with { kids ->
        kids.findAll { it instanceof Node ? !cleanNode( it ) : false }
            .each { kids.remove( it ) }
    }
    node.attributes() || node.children() || node.text()
}




static main(args) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder parser = factory.newDocumentBuilder();
    Document xmlDoc = parser.parse("C:/Users/Myuser/Desktop/Testxml2.xml");//file has the xml*/
    String xml = nodeToString(xmlDoc.getDocumentElement());
    def root = new XmlParser().parseText( xml );
    cleanNode(root)
    def test = XmlUtil.serialize(root);
    println test;
}

static String nodeToString(Node node) {
    StringWriter sw = new StringWriter();
    try {
      Transformer t = TransformerFactory.newInstance().newTransformer();
      t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
      t.setOutputProperty(OutputKeys.INDENT, "yes");
      t.transform(new DOMSource(node), new StreamResult(sw));
    } catch (TransformerException te) {
      System.out.println("nodeToString Transformer Exception");
    }
    return sw.toString();
}

But This is not working because Telephone and Team are not deleted

This is output

<Profile>
        <Name>Fin</Name>
        <Aga>20</Aga>
        <Contact>
            <Mobile>1819</Mobile>
            <Telephone/>
        </Contact>
        <Team>
            <Team1/>
            <Team2/>
        </Team>
  </Profile>

This is Expected Result

<Profile>
    <Name>Fin</Name>
    <Aga>20</Aga>
    <Contact>
        <Mobile>1819</Mobile>
    </Contact>
</Profile>

Could your please tell me about this?

You can do as mentioned below:

  • find all node names where node text is empty
  • loop thru the above find result, and remove node
def xmlString = """<Profile>
    <Name>Fin</Name>
    <Aga>20</Aga>
    <Contact>
        <Mobile>1819</Mobile>
        <Telephone/>
    </Contact>
    <Team>
        <Team1/>
        <Team2/>
    </Team>
</Profile>"""

def xml = new XmlParser().parseText(xmlString)
def nodes = xml.'**'.findAll{it.name() && !it.text()}
def removeNode = { node ->
   def parent = node.parent()
   parent.remove(node)
}

nodes.each{removeNode(it)}
println groovy.xml.XmlUtil.serialize(xml)

You can also quickly try the same on-line demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM