Results 1 to 3 of 3
  1. #1
    Mr tuition is offline Member
    Join Date
    Dec 2007
    Posts
    12
    Rep Power
    0

    Default how to remove all of the attributes from XML Document using JAVA

    Hi there,

    this is for a personal project I'm working on. I have lots of xml files that I want to stip down to a very simplified format (and manuipulate some elements). As part of this stripping down, I need to remove all of the XML attibutes at some point.

    I'm trying to write a method that will strip out all of the attributes from my XML Document (going through each child, child-child ... etc in turn)
    The way I'm doing it is clearly wrong, but I havent figured out a better way to do it yet. If anyone could point me in the right direction.
    As you can see, I will keep cycling through for ever and this is huge and currently only goes 4 nodes deep, is there a better way?

    The method I've create so far is as follows:


    Java Code:
    public Document removeAllAttributes(Document thisDoc){
      NodeList nodeList = thisDoc.getChildNodes();
      for(int i=0; i<nodeList.getLength(); i++){
        Node childNode = nodeList.item(i);
        NamedNodeMap namedNodeMap = childNode.getAttributes(); // this is a list of Attributes (if there are any)			
        // remove attributes for child
        for(int j=0; j<namedNodeMap.getLength(); j++){
          Node childAttribute = namedNodeMap.item(j);
          thisDoc.getChildNodes().item(i).getAttributes().removeNamedItem(childAttribute.getNodeName());
        }
        
        NodeList childNodeList = childNode.getChildNodes();
        for(int k=0; k<childNodeList.getLength(); k++){
          if(childNodeList.item(k).getNodeName()!="#text"){
            Node granChildNode = childNodeList.item(k);
            NamedNodeMap chileNamedNodeMap = granChildNode.getAttributes(); // this is a list of Attributes (if there are any)			
    	for(int l=0; l<chileNamedNodeMap.getLength(); l++){
    	  Node granChildAttribute = chileNamedNodeMap.item(l);
    	  thisDoc.getChildNodes().item(i).getChildNodes().item(k).getAttributes().removeNamedItem(granChildAttribute.getNodeName());
            }
    
            NodeList gChildNodeList = granChildNode.getChildNodes();
    	for(int m=0; m<gChildNodeList.getLength(); m++){
    	  if(gChildNodeList.item(m).getNodeName()!="#text"){
    	    Node ggranChildNode = gChildNodeList.item(m);
    	    NamedNodeMap gchildNamedNodeMap = ggranChildNode.getAttributes(); 			
    	    for(int n=0; n<gchildNamedNodeMap.getLength(); n++){
    	      Node ggChildAttribute = gchildNamedNodeMap.item(n);
    	      thisDoc.getChildNodes().item(i).getChildNodes().item(k).getChildNodes().item(m).getAttributes().removeNamedItem(ggChildAttribute.getNodeName());
    	    }
    
                NodeList ggChildNodeList = ggranChildNode.getChildNodes();
    	    for(int o=0; o<ggChildNodeList.getLength(); o++){
    	      if(ggChildNodeList.item(o).getNodeName()!="#text"){
    		Node gggranChildNode = gChildNodeList.item(o);
    		NamedNodeMap ggchildNamedNodeMap = gggranChildNode.getAttributes(); 			
    		for(int p=0; p<ggchildNamedNodeMap.getLength(); p++){
    		  Node gggChildAttribute = ggchildNamedNodeMap.item(p);
    		  thisDoc.getChildNodes().item(i).getChildNodes().item(k).getChildNodes().item(m).getChildNodes().item(o).getAttributes().removeNamedItem(gggChildAttribute.getNodeName());
    		
                    // AT THIS POINT I'M ONLY 4 LEVELS DEEP, and you can see already the above is looking silly.
                    }
    	      }	
    	    }	
    	  }	
            }
          }	
        }
      }
      return thisDoc;
    }
    Last edited by Mr tuition; 04-02-2011 at 10:58 PM.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    10

    Default

    Selecting all nodes which have attributes with XPath and then you can use removeNamedItem.
    Try: (not tested)
    Java Code:
    	public static Document removeAllAttributes(Document thisDoc) throws XPathExpressionException {
    		XPathFactory factory = XPathFactory.newInstance();
    		XPath xpath = factory.newXPath();
    		XPathExpression expr = xpath.compile("//*[@*]");
    		NodeList result =(NodeList) expr.evaluate(thisDoc, XPathConstants.NODESET);
    		for (int i = 0; i < result.getLength(); i++) {
    			NamedNodeMap map = result.item(i).getAttributes();
    			for (int j = 0; j < map.getLength(); j++) {
    				map.removeNamedItem(map.item(j--).getNodeName());
    			}
    		}
    		return thisDoc;
    	}

  3. #3
    Mr tuition is offline Member
    Join Date
    Dec 2007
    Posts
    12
    Rep Power
    0

    Default

    You beauty :D

    I hadnt thought of using XPath, I've hardly ever used it before. Going though the Dom the way I was, clearly wasnt right.

    Using XPath makes xml manipulation so much easier

    Thanks eRaaaa, rep added ;)

Similar Threads

  1. Get CSS Attributes using Java
    By justinmifsud in forum Advanced Java
    Replies: 0
    Last Post: 09-20-2010, 04:11 PM
  2. execute a .exe with attributes from java
    By danielleon11 in forum Advanced Java
    Replies: 1
    Last Post: 07-14-2010, 01:25 AM
  3. Replies: 0
    Last Post: 01-20-2008, 06:07 AM
  4. Replies: 2
    Last Post: 12-04-2007, 10:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •