Example usage for javax.xml.namespace QName getLocalPart

List of usage examples for javax.xml.namespace QName getLocalPart

Introduction

In this page you can find the example usage for javax.xml.namespace QName getLocalPart.

Prototype

public String getLocalPart() 

Source Link

Document

Get the local part of this QName.

Usage

From source file:Main.java

public static String serializeQName(QName qname) {
    String prefix = qname.getPrefix();
    if (prefix.isEmpty()) {
        return qname.getLocalPart();
    }/*from   w  ww.  j  a  v a  2 s  .  c  om*/
    return prefix + ":" + qname.getLocalPart();
}

From source file:Utils.java

/**
 * Return the first element child with the specified qualified name.
 * /*from   w w w.  j  av  a 2 s  . c  o  m*/
 * @param parent
 * @param q
 * @return
 */
public static Element getFirstChildWithName(Element parent, QName q) {
    String ns = q.getNamespaceURI();
    String lp = q.getLocalPart();
    return getFirstChildWithName(parent, ns, lp);
}

From source file:Main.java

/**
 * @param key Fully qualified element name.
 *///from   w ww  .jav a  2 s .  c om
public static Element createElement(QName key) {
    return createDocument().createElementNS(key.getNamespaceURI(), key.getPrefix() + ":" + key.getLocalPart());
}

From source file:Main.java

/**
 * Static utility method that throws an exception if the supplied reader's
 * cursor doesn't point to a START_ELEMENT with the given name.
 * /*from   w  w w.  j av a 2s.c  o  m*/
 * @param reader The reader to test.
 * @param name The name of the element to require.
 * @throws XMLStreamException If the reader state is an element with the
 *             specified name.
 */
public static final void requireElement(XMLStreamReader reader, QName name) throws XMLStreamException {

    reader.require(XMLStreamReader.START_ELEMENT, name.getNamespaceURI(), name.getLocalPart());

}

From source file:Main.java

public static String getAttributeNSOrNull(Element e, QName name) {
    Attr a = e.getAttributeNodeNS(name.getNamespaceURI(), name.getLocalPart());
    if (a == null)
        return null;
    return a.getValue();
}

From source file:Main.java

/**
 * Find a Node with a given QName/* ww w . j  a v a  2 s  . c  o  m*/
 *
 * @param node parent node
 * @param name QName of the child we need to find
 * @return child node
 */
public static Node findNode(Node node, QName name) {
    if (name.getNamespaceURI().equals(node.getNamespaceURI())
            && name.getLocalPart().equals(node.getLocalName()))
        return node;
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node ret = findNode(children.item(i), name);
        if (ret != null)
            return ret;
    }
    return null;
}

From source file:Main.java

public static Element getFirstChildElementNS(Element domNode, QName name) {
    return getFirstChildElementNS(domNode, name.getNamespaceURI(), name.getLocalPart());
}

From source file:Main.java

/**
 * Finds a Node with a given QNameb./*from w w w  . j a v a2 s  .c om*/
 *
 * @param node parent node
 * @param name QName of the child we need to find
 * @return Returns child node.
 */
public static Node findNode(Node node, QName name) {
    if (name.getNamespaceURI().equals(node.getNamespaceURI())
            && name.getLocalPart().equals(node.getLocalName())) {
        return node;
    }
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node ret = findNode(children.item(i), name);
        if (ret != null) {
            return ret;
        }
    }
    return null;
}

From source file:Main.java

/**
 * Searches through an entire sub-tree for child Elements whose QName
 * matches the one given. The results are not guaranteed to be in any
 * particular order, and are <b>not</b> limited to direct children of
 * the given context node. <br>//from   w  ww  .  j  a v a2s . co  m
 * <br>
 * If you want to limit your search for Elements to the direct children
 * of a given Element, use the getAllElement, getElement, and getElements
 * methods. Those guarantee that all results are directly beneath the
 * given context.
 * 
 * @param context
 *            The root node from which the search will be done.
 * @param qname
 *            The QName of the Element to search for.
 * @return An array with all occurrences of child Elements with the
 *         given name. The Elements may be anywhere in the sub-tree,
 *         meaning they may not be direct children of the context node.
 *         The only guarantee is that they are somewhere beneath the
 *         context node. The method returns an empty array if no
 *         occurrences are found.
 * @see Element#getElementsByTagNameNS(String, String)
 * @see #getAllElements(Node)
 * @see #getElement(Node, QName)
 * @see #getElements(Node, QName)
 */
public static Element[] findInSubTree(final Element context, final QName qname) {
    final String name = qname.getLocalPart();
    final String uri = qname.getNamespaceURI();

    //
    // DOM's getElementsByTagName methods search the whole subtree
    //
    final NodeList matches = context.getElementsByTagNameNS(uri, name);
    final int length = matches.getLength();

    final Element[] asArray = new Element[length];

    for (int n = 0; n < length; ++n)
        asArray[n] = (Element) matches.item(n);

    return asArray;
}

From source file:Main.java

/**
 * @param doc//w w  w  .  ja  v  a  2 s. c  o m
 *            The Document that is the factory for the new Element.
 * @param qname
 *            The QName of the new Element.
 * @return An empty Element whose owner is the given Document.
 */
private static Element createEmptyElement(final Document doc, final QName qname) {
    final String prefix = qname.getPrefix();
    String name = qname.getLocalPart();

    //
    // NOTE: The DOM API requires that elements with qualified names 
    //       be created with prefix:localName as the tag name. We 
    //       CANNOT just use the localName and expect the API to fill 
    //       in a prefix, nor can we use setPrefix after creation. For 
    //       parsing to work correctly, the second argument to the 
    //       createElementNS method MUST be a qualified name.
    //
    if (prefix != null && prefix.length() > 0)
        name = prefix + ':' + name;

    final String uri = qname.getNamespaceURI();

    return doc.createElementNS(uri, name);
}