Java Utililty Methods XML Node Namespace

List of utility methods to do XML Node Namespace

  1. HOME
  2. Java
  3. X
  4. XML Node Namespace

Description

The list of methods to do XML Node Namespace are organized into topic(s).

Method

Node convertFromNamespaceForm(final Node node)
convert From Namespace Form
if (node instanceof Element) {
 final Document document = node.getOwnerDocument();
 final Element newElement = document.createElementNS(null, node.getLocalName());
 final NodeList children = node.getChildNodes();
 for (int i = 0, n = children.getLength(); i < n; i++) {
 final Node oldChildNode = children.item(i);
 final Node newChildNode = convertFromNamespaceForm(oldChildNode);
 newElement.appendChild(newChildNode);
...
void copyNamespaces(Node source, Node target)
Copies namespace attributes from a source, recursing its parents, to target node.
if (!(target instanceof Element)) {
 return;
Element targetElement = (Element) target;
Node parent = source.getParentNode();
while (parent != null) {
 copyNamespaces(parent, targetElement);
 parent = parent.getParentNode();
...
String getNamespace(Node node)
get Namespace
Element root = node.getOwnerDocument().getDocumentElement();
HashMap<String, String> namespaces = getAllAttributes(root);
String prefix = getPrefix(node.getNodeName());
if (prefix == null)
 return namespaces.get("xmlns");
return namespaces.get("xmlns:" + prefix);
String getNamespace(Node node)
Gets the namespace of the given node.
String name = node.getNodeName();
int colon = name.lastIndexOf(":");
return colon < 0 ? null : name.substring(0, colon);
String getNamespace(String prefix, Node e)
get Namespace
return getNamespace(prefix, e, null);
Map getNamespaceDeclarations(Node node)
Build the namespace prefix to namespace URL mapping in effect for a given node.
Map nsDecls = new HashMap();
int i;
do {
 if (node.hasAttributes()) {
 NamedNodeMap attrs = node.getAttributes();
 for (i = 0; i < attrs.getLength(); i++) {
 Attr attr = (Attr) attrs.item(i);
 if ("xmlns".equals(attr.getPrefix()) || "xmlns".equals(attr.getName())) {
...
Map getNamespaceMappings(Node node)
Get all the name spaces visible from this node.
Map<String, String> namespaces = new HashMap<String, String>();
boolean firstLoop = true;
Node n = node;
while (n != null) {
 if (firstLoop && n.getNamespaceURI() != null) {
 namespaces.put("", n.getNamespaceURI());
 firstLoop = false;
 if (n.getAttributes() != null) {
 for (int i = 0; i < n.getAttributes().getLength(); i++) {
 String attrName = n.getAttributes().item(i).getNodeName();
 if (attrName.startsWith("xmlns")) { 
 String prefix = "";
 int index = attrName.indexOf(':');
 if (index != -1 && ++index < attrName.length())
 prefix = attrName.substring(index);
 String uri = n.getAttributes().item(i).getNodeValue();
 namespaces.put(prefix, uri);
 n = n.getParentNode();
return namespaces;
void getNamespaces(Node node, Map list)
get Namespaces
NamedNodeMap atts = node.getAttributes();
for (int i = 0; i < atts.getLength(); i++) {
 Node n = atts.item(i);
 if ("xmlns".equals(n.getNodeName())) {
 list.put(n.getNodeName(), n.getNodeValue());
 } else {
 if (n.getNodeName().startsWith("xmlns:")) {
 list.put(n.getNodeName().substring(6), n.getNodeValue());
...
String getNamespaceURI(@Nullable final Node aNode)
get Namespace URI
if (aNode instanceof Document)
 return getNamespaceURI(((Document) aNode).getDocumentElement());
if (aNode != null)
 return aNode.getNamespaceURI();
return null;
String getNamespaceURI(final org.w3c.dom.Node n, final String prefix)
get Namespace URI
final Node prefixDeclaration = n.getAttributes().getNamedItem("xmlns:" + prefix);
if (prefixDeclaration != null) {
 return prefixDeclaration.getNodeValue();
return getNamespaceURI(n.getParentNode(), prefix);


AltStyle によって変換されたページ (->オリジナル) /