List of utility methods to do XML Element Namespace
void | addNamespaceDeclaration(Element element, String namespacePrefix, String namespaceURI) Adds a namespace declaration attribute to the given element. element.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
XMLConstants.XMLNS_ATTRIBUTE + ':' + namespacePrefix, namespaceURI);
|
void | addNamespacePrefix(Element element, String namespaceUri, String prefix) Add a namespace prefix definition to an element. element.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:" + prefix, namespaceUri);
|
Map | buildNamespacePrefixMap(final Element root) Build Map of namespace URIs to prefixes. final HashMap<String, String> namespacePrefixMap = new HashMap<>(); NamedNodeMap attributes = root.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { Node item = attributes.item(i); if (item.getNodeName().startsWith("xmlns")) { String[] splitName = item.getNodeName().split(":"); String prefix; if (splitName.length > 1) { ... |
Map | buildNamespacePrefixMap(final Element root) Build Map of namespace URIs to prefixes. final HashMap<String, String> namespacePrefixMap = new HashMap<>(); NamedNodeMap attributes = root.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { Node item = attributes.item(i); if (item.getNodeName().startsWith("xmlns")) { String[] splitName = item.getNodeName().split(":"); String prefix; if (splitName.length > 1) { ... |
Element | byteArrayToElement(byte[] b, boolean namespaceAware) Convert the byte array representation of an Element into an Element DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); docBuilderFactory.setNamespaceAware(namespaceAware); docBuilderFactory.setValidating(false); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document doc = docBuilder.parse(new ByteArrayInputStream(b)); return doc.getDocumentElement(); |
Element | createElement(final String namespace, final String localPart, final String value) create Element if (document == null) { throw new RuntimeException("XMLDocumentBuilder could not be initialized"); Element element = document.createElementNS(namespace, "ns:" + localPart); element.appendChild(document.createTextNode(value)); return element; |
Element | createElement(String namespaceURI, String name) create Element DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setNamespaceAware(true); try { final DocumentBuilder builder = builderFactory.newDocumentBuilder(); final Document document = builder.newDocument(); return document.createElementNS(namespaceURI, name); } catch (ParserConfigurationException e) { throw new RuntimeException("Couldn't get a DocumentBuilder", e); ... |
Element | createElement(String namespaceURI, String name) create Element return getDocument().createElementNS(namespaceURI, name);
|
String | createNamespace(Element el, String ns) create Namespace String p = "ns1"; int i = 1; while (getPrefix(el, ns) != null) { p = "ns" + i; i++; addNamespacePrefix(el, ns, p); return p; ... |
List | getAllNamespaceDeclarations(Element element) get All Namespace Declarations List<QName> nodes = new ArrayList<>(); NamedNodeMap map = element.getAttributes(); for (int i = 0; i < map.getLength(); i++) { Node node = map.item(i); String nsUri = node.getNamespaceURI(); if (nsUri == null) { continue; if ("http://www.w3.org/2000/xmlns/".equals(nsUri)) { nodes.add(new QName(node.getTextContent(), node.getLocalName(), "")); return nodes; |