Java Utililty Methods XML QName

List of utility methods to do XML QName

Description

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

Method

Objectread(Node node, String expression, QName returnType)
read
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression xPathExp = xpath.compile(expression);
return xPathExp.evaluate(node, returnType);
StringreadTextElement(XMLEventReader reader, QName elemName)
Reads the text content of an element.
if (elemName != null) {
    requireStartElement(reader, elemName);
String text = reader.getElementText();
reader.nextEvent();
return text;
StringresolveNamespace(String qname, Node xml)
Parses the prefix from the given qualified name and finds the first XML namespace declaration that maps that prefix to a namespace URI.
int colonIndex = qname.indexOf(':');
String attributeName = XMLNS_PREFIX;
if (colonIndex >= 0) {
    String prefix = qname.substring(0, colonIndex);
    attributeName += ':' + prefix;
return searchParentNamespaces(xml, attributeName);
QNameresolveQName(final Element el, final String qualifiedName)
resolve Q Name
QName qname;
String prefix = "";
String namespaceURI = "";
String localPart = qualifiedName;
final int colIndex = qualifiedName.indexOf(":");
if (colIndex > 0) {
    prefix = qualifiedName.substring(0, colIndex);
    localPart = qualifiedName.substring(colIndex + 1);
...
QNameresolveQName(String qNameWithPrefix, Element element)
resolve Q Name
String nsPrefix;
String localName;
if (qNameWithPrefix.contains(":")) {
    String[] parts = qNameWithPrefix.split(":");
    nsPrefix = parts[0];
    localName = parts[1];
} else {
    nsPrefix = "";
...
voidsearch(List list, Element baseElement, QName nodeName, boolean recursive)
search
if (nodeName == null) {
    list.add(baseElement);
} else {
    QName qname;
    if (nodeName.getNamespaceURI().length() > 0) {
        qname = new QName(baseElement.getNamespaceURI(), baseElement.getLocalName());
    } else {
        qname = new QName(baseElement.getLocalName());
...
SerializableserializeJavaToXml(QName qname, Serializable value)
serialize Java To Xml
Serializable xmlData = null;
if (value != null) {
    Class clazz = value.getClass();
    JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
    Marshaller marshaller = jaxbContext.createMarshaller();
    Writer out = new StringWriter();
    JAXBElement<Serializable> element = new JAXBElement<Serializable>(qname, clazz, value);
    marshaller.marshal(element, out);
...
StringserializeQName(QName qName)
serialize Q Name
if (qName == null) {
    return "";
return qName.getNamespaceURI() + "#" + qName.getLocalPart();
voidsetValue(java.util.Map map, String name, String newValue)
set Value
Optional<java.util.Map.Entry<QName, String>> attr = map.entrySet().stream()
        .filter((e) -> e.getKey().getLocalPart().equals(name)).findFirst();
if (attr.isPresent()) {
    attr.get().setValue(newValue);
} else {
    map.put(new QName(name), newValue);
String[]splitQName(String qualifiedName)
split Q Name
if (qualifiedName == null) {
    throw new IllegalArgumentException("Null QName");
int idx = qualifiedName.indexOf(':');
String[] parts = { null, null };
if (idx >= 0) {
    parts[0] = qualifiedName.substring(0, idx);
    parts[1] = qualifiedName.substring(idx + 1);
...