Solved

Remove attribute form XML in java

Posted on 2014-12-18
1
503 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

762 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