Solved

Remove attribute form XML in java

Posted on 2014-12-18
1
527 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
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 theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

738 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