ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button
Article:
  Parsing an XML Document with XPath
Subject:   Parsing a Namespace node with JDK 5.0
Date:   2005-02-17 08:01:53
From:   dvohra09
Response to: Parsing a Namespace node with JDK 5.0

Develop an implementation class of the NamespaceContext interface with the provision to set more than one prefixes for a uri.


import javax.xml.namespace.NamespaceContext;
import java.util.Iterator;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.util.ArrayList;
import java.util.List;


public class MyNamespaceContext implements NamespaceContext {
private Map map;


public MyNamespaceContext() {
map = new HashMap();
}


public void setNamespace(String prefix, String namespaceURI) {
map.put(prefix, namespaceURI);
}


public String getNamespaceURI(String prefix) {
return (String) map.get(prefix);
}


public String getPrefix(String namespaceURI) {
Set keys = map.keySet();
for (Iterator iterator = keys.iterator(); iterator.hasNext();)
{
String prefix = (String) iterator.next();
String uri = (String) map.get(prefix);
if (uri.equals(namespaceURI)) return prefix;
}
return null;
}


public Iterator getPrefixes(String namespaceURI) {
List prefixes = new ArrayList();
Set keys = map.keySet();
for (Iterator iterator = keys.iterator(); iterator.hasNext();)
{
String prefix = (String) iterator.next();
String uri = (String) map.get(prefix);
if (uri.equals(namespaceURI)) prefixes.add(prefix);
}
return prefixes.iterator();
}
}



1 to 1 of 1
  1. Parsing a Namespace node with JDK 5.0
    2005-02-18 02:16:18  Gerg [View]

    • Parsing a Namespace node with JDK 5.0
      2006-02-22 09:15:26  Gerg [View]

1 to 1 of 1