public final class XmlUtils extends Object
Modifier and Type | Class and Description |
---|---|
static class |
XmlUtils.CachingContentHandler
Allows clients to cache SAX events directly rather than, say, caching an XML String and then
instantiating a new SAX Parser each time to parse it.
|
Modifier and Type | Method and Description |
---|---|
static void |
combineElements(Element master,
Element toAdd,
String topLevelAttributeToCombineOn,
String childAttributeToCombineOn)
Combine the attributes and child elements of the second element into the first element.
|
static Document |
documentFromString(String xml)
Converts the given XML into a
org.w3c.dom.Document . |
static String |
documentToString(Document document,
boolean pretty)
Convert the given Document to an XML String.
|
static Map<String,String> |
getAttributesAsMap(Node node)
Gets the DOM attributes of the given Node as a Map.
|
static Element |
getChildNamed(Element element,
String... names)
Looks up a child with the given (local) name.
|
static Element |
getChildWithAttribute(Element element,
String attributeName) |
static Element |
getChildWithAttributeValue(Element element,
String attributeName,
String attributeValue) |
static Element |
getFirstChildElement(Element parent)
Get the indexed Element.
|
static String |
getLocalName(Node node)
Gets the localName of the given node.
|
static Element |
getNextSiblingElement(Element element) |
static Element |
getSiblingNamed(Element element,
String name)
Gets the next sibling to the given element with the given name.
|
static Element |
getSiblingWithAttribute(Element element,
String attributeName)
Gets the next sibling to the given element with the given attribute.
|
static Element |
importElement(Document document,
Element element) |
static String |
inspectionResultToJsonSchema(Element inspectionResult)
Convert the given Element to a JSON Schema (v3) String.
|
static Document |
newDocument()
Creates a new Document built from a shared, no-external-connection-making DocumentBuilder
created by a namespace-aware, comment-ignoring, whitespace-ignoring DocumentBuilderFactory.
|
static String |
nodeToString(Node node,
boolean pretty)
See documentToString.
|
static Document |
parse(InputStream stream) |
static void |
setMapAsAttributes(Element element,
Map<String,String> attributes)
Sets the Map as DOM attributes on the given Element.
|
public static Map<String,String> getAttributesAsMap(Node node)
public static void setMapAsAttributes(Element element, Map<String,String> attributes)
This implementation uses element.setAttribute
. Therefore if the element already
has attributes, the new attributes are added amongst them. If attributes with the same name
already exist, they are overwritten. To remove attributes from the given Element,
put them in the Map with a null
value.
public static Element getChildNamed(Element element, String... names)
An array of several names may be passed, in which case they will be traversed in a simple XPath-like fashion.
public static Element getChildWithAttribute(Element element, String attributeName)
public static Element getChildWithAttributeValue(Element element, String attributeName, String attributeValue)
public static Element getSiblingNamed(Element element, String name)
public static Element getSiblingWithAttribute(Element element, String attributeName)
public static String documentToString(Document document, boolean pretty)
This method is a simplified version of...
ByteArrayOutputStream out = new ByteArrayOutputStream();
javax.xml.Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform( new DOMSource( node ), new StreamResult( out ));
return out.toString();
...but not all platforms (eg. Android) support javax.xml.transform.Transformer
.
public static Document documentFromString(String xml)
org.w3c.dom.Document
.
Named documentFromString
, rather than just parse
, because
DocumentBuilder.parse( String )
already exists and, confusingly, uses the String
as a URI to the XML rather than as the XML itself.
Note: in performance tests, this method was consistently found to be expensive, of the order
of around 10%. Consider implementing DomInspector
on your
Inspectors
or DomInspectionResultProcessor
on your
InspectionResultProcessors
to avoid this hit.
public static Document newDocument()
public static Document parse(InputStream stream) throws IOException, SAXException
IOException
SAXException
public static Element getFirstChildElement(Element parent)
Similar to Element.getChildNodes.item
, but ignores any Nodes (such as
indentation TextNodes).
public static void combineElements(Element master, Element toAdd, String topLevelAttributeToCombineOn, String childAttributeToCombineOn)
Combining is performed purely by matching a topLevelAttributeToCombineOn attribute on the element. The child element ordering of the first element is respected.
Child elements are matched recursively on childAttributeToCombineOn.
public static String inspectionResultToJsonSchema(Element inspectionResult)
If converting inspection-result
DOMs into JSON Schemas, consider using this
method in conjunction with JsonSchemaMappingProcessor
and
JsonTypeMappingProcessor
. The result is directly compatible with the JavaScript
versions of Metawidget. It can therefore be returned by REST services (see
http://blog.kennardconsulting.com/2013/02/metawidget-and-rest.html).
Copyright © 2015. All Rights Reserved.