Solved

Remove attribute form XML in java

Posted on 2014-12-18
1
415 Views
Last Modified: 2015-01-08
How can I remove an attribute from XML? Below code is not working.

	private static Document removeXMLNSAttribute(Document doc) throws ParserConfigurationException, SAXException, IOException {
	    NodeList nodeList = doc.getElementsByTagName("*");
	    for ( int i = 0; i < nodeList.getLength(); i++ ) {
	    	Element ele = (Element) nodeList.item(i);
	    	if (ele.hasAttribute("xmlns")) {
	    		ele.removeAttribute("xmlns");
	    	}
	    }
	    return doc;
	}

Open in new window


The return value of above function still contain 'xmlns' attribute.
0
Comment
Question by:Ravi Kalla
1 Comment
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 40514780
I'm not the biggest java/xml expert (by far) but since they seem to be on holiday here is some working code for you, but to be honest I doubt it's the best way:
	private static Document removeXMLNSAttribute(Document doc) throws ParserConfigurationException, SAXException, IOException {
		NodeList nodeList = doc.getElementsByTagName("*");
		for ( int i = 0; i < nodeList.getLength(); i++ ) {
			if (nodeList.item(i).getNodeType() == Node.ELEMENT_NODE) {
				Element ele = (Element) nodeList.item(i);
				NamedNodeMap nnm = ele.getAttributes();
				for (int a = nnm.getLength() - 1; a >= 0; a--) { // back to front because of remove in loop!
					Attr attr = (Attr)nnm.item(a);
					if (attr.getNodeName().startsWith("xmlns:")) {
						ele.removeAttributeNode(attr);
					}
				}
			}
		}
		return doc;
	}

Open in new window

For more information see for example http://www.w3.org/2003/01/dom2-javadoc/org/w3c/dom/Node.html#getAttributes__
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now