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
Solved

Remove attribute form XML in java

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.

809 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