Java XML Duration Add addNamespacePrefix(Element parent, String namespace, String basePrefix)

Here you can find the source of addNamespacePrefix(Element parent, String namespace, String basePrefix)

Description

Use the basePrefix to define new name-space prefix If the basePrefix is already used for other name-space, a number will be added to the prefix to create a new unique prefix

License

Open Source License

Parameter

Parameter Description
parent a parameter
namespace a parameter
basePrefix a parameter

Return

the prefix that is now associated with this name-space.

Declaration

public static String addNamespacePrefix(Element parent, String namespace, String basePrefix) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Crown Copyright (c) 2006, 2007, Copyright (c) 2006, 2007 Jiva Medical.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * /*from w ww .  ja va 2s.c  o m*/
 * Contributors:
 *    Jiva Medical - initial API and implementation
 *    B2 International - added generateXMLString(DocumentFragment, boolean)
 *******************************************************************************/

import org.w3c.dom.Attr;

import org.w3c.dom.Element;

public class Main {
    /**
     * Use the basePrefix to define new name-space prefix
     * If the basePrefix is already used for other name-space, a number will be added to the prefix to create
     * a new unique prefix
     * 
     * @param parent
     * @param namespace
     * @param basePrefix
     * @return the prefix that is now associated with this name-space.
     */
    public static String addNamespacePrefix(Element parent, String namespace, String basePrefix) {
        String result = suggestNamespacePrefix(parent, namespace, basePrefix);

        parent.getOwnerDocument().getDocumentElement().setAttribute("xmlns:" + result, namespace);
        return result;
    }

    /**
     * To find unallocated name-space prefix that can be used to define given name-space.
     * This method often call with addNamespacePrefix;
     * 
     * @param parent
     * @param namespace
     * @param basePrefix
     * @return
     */
    public static String suggestNamespacePrefix(Element parent, String namespace, String basePrefix) {
        String result = basePrefix;
        if (result == null)
            result = "ns";
        int count = 0;
        String foundNS = lookupPrefixOnElement(parent, basePrefix);
        while (foundNS != null && !foundNS.equals(namespace)) {
            count++;
            result = basePrefix + count;
        }
        return result;
    }

    /**
     * @param element
     * @param prefix
     * @return the name-space corresponding to this prefix, or null
     */
    public static String lookupPrefixOnElement(Element element, String prefix) {

        for (int i = 0; i < element.getAttributes().getLength(); i++) {
            Attr attr = (Attr) element.getAttributes().item(i);
            String name = attr.getNodeName();

            if (name.startsWith("xmlns:") && name.endsWith(":" + prefix)) {
                return attr.getNodeValue();
            }
        }
        return null;
    }
}

Related

  1. addElement(Node parent, String name)
  2. addElement(String name, Element parent)
  3. addElementFirst(final Element parent, final String name)
  4. addElementFirst(final Element parent, final String name)
  5. addLongElementNS(Element parent, String namespaceURI, String tag, long value)
  6. addNode(Element parent, String elementName, String namespaceURI)
  7. addNode(Node parentNode, String name)
  8. addText(Node parent, String value)
  9. addTextElement(Node parent, String name, String value)