|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.kuka.common.XmlUtil
public final class XmlUtil
Merging algorithm for XML configuration files.
| Field Summary | |
|---|---|
static String |
XMLATTR_MERGE
Attribute name to use for defining the attributes to be set or replaced in XML files being merged. |
static String |
XMLNS_MERGE
XML namespace for merge operations allowing to replace existing tags in XML files being merged. |
| Method Summary | |
|---|---|
static void |
createAttribute(Node node,
String name,
String value)
Creates an attributes for the given node. |
static ParameterSet |
createParameterSetFromNode(Node node)
Creates and loads a parameter set out of a Xml node. |
static Document |
deleteXmlElements(Document original,
Document elementsToBeDelete)
Delete XML data listed in ElementsToBeDelete from the original document. |
static Node |
getChildNode(String childName,
Node node)
Gets a names child node. |
static Node |
getOptionalChildNode(String childName,
Node node)
Gets a named child node. |
static Element |
getSingleElement(Element parent,
String elementName)
gets the first element with the specified name. |
static Document |
getXmlDocument(File file)
reads a XML file by its file. |
static Document |
getXmlDocument(InputStream input)
reads XML data from an input stream. |
static Document |
getXmlDocument(String filename)
reads a XML file by its filename. |
static Document |
getXmlDocument(URL url)
imports the xml file into a document. |
static Document |
getXmlDocumentFromString(String input)
Reads XML data from the given string. |
static boolean |
hasAttribute(String name,
Node node)
Checks if the given node has the specified attribute. |
static Document |
mergeXmlElements(Document original,
Document template)
Merges the XML data from template into the original document. |
static DocumentBuilder |
newBuilder()
Returns the DocumentBuilder. |
static Document |
newDocument()
|
static String |
optionalAttribute(String name,
Node node,
String defaultValue)
Gets a attribute from a node. |
static void |
removeIgnorableWhitespace(Document doc)
Removes all ignorable whitespace from the element, as defined per the XML spec. |
static void |
removeIgnorableWhitespace(Element e)
Removes all ignorable whitespace from the element, as defined per the XML spec. |
static Document |
replaceVariablesAndParse(URL url)
Loads the document from the given file and replaces contains VAR tags. |
static String |
requiredAttribute(String name,
Node node)
Gets a attribute from a node. |
static void |
validateXmlDocument(File xmlFile,
URL schemaUrl)
validates a XML file - based on the given XML schema the structure of the XML file will be validated. |
static void |
writeDocumentToXml(Document doc,
File file)
Writes the given document to the specified xml file. |
static void |
writeDocumentToXml(Document doc,
String filename)
Writes the given document to the specified xml file. |
static void |
writeXmlFile(Document doc,
String filename)
Deprecated. Use writeDocumentToXml(Document doc, String filename) instead |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final String XMLNS_MERGE
mergeXmlElements(Document, Document),
Constant Field Valuespublic static final String XMLATTR_MERGE
XMLNS_MERGE.
| Method Detail |
|---|
public static DocumentBuilder newBuilder()
public static Document newDocument()
public static Document getXmlDocument(File file)
throws SAXException,
IOException
file - file to read
SAXException - error parsing the XML document
IOException - error reading the XML document
public static Document getXmlDocument(String filename)
throws SAXException,
IOException
filename - name of the XML file
SAXException - error parsing the XML document
IOException - error reading the XML document
public static Document getXmlDocument(InputStream input)
throws SAXException,
IOException
input - stream with XML data
SAXException - error parsing the XML document
IOException - error reading the XML document
public static Document getXmlDocument(URL url)
throws SAXException,
IOException
url - The url where the xml file can be found
SAXException - error parsing the XML document
IOException - error reading the XML document
public static Document getXmlDocumentFromString(String input)
throws SAXException,
IOException
input - string containing an XML document.
SAXException - if XML is not correct.
IOException - if string can't be read correctly.
public static void validateXmlDocument(File xmlFile,
URL schemaUrl)
throws SAXException,
IOException
xmlFile - XML file to validateschemaUrl - XML schema file, which is used for the validation
SAXException - the validation process has detected an error in the XML file or a general parser error has been
occurred (either within the XML file or the XML schema file)
IOException - error reading the XML document or the XML schema
@Deprecated
public static void writeXmlFile(Document doc,
String filename)
throws TransformerException
writeDocumentToXml(Document doc, String filename) instead
doc - XML documentfilename - name of the XML file to write
TransformerException - error transforming XML data
public static void writeDocumentToXml(Document doc,
File file)
doc - The document to be written.file - The file to which the document writes it's contents.
public static void writeDocumentToXml(Document doc,
String filename)
doc - The document to be written.filename - The filename to which the document writes it's contents.
public static Document deleteXmlElements(Document original,
Document elementsToBeDelete)
original - the original document where XML element should be removedelementsToBeDelete - the XML elements that should be remove from the original document
public static Document mergeXmlElements(Document original,
Document template)
template into the original document. Each element and attribute in
template is matched against existing elements and attributes in original. The merge mechanism
recursively works as follows:
template, all elements of original with the same name and the same
hierarchical position will match if all attributes given in the template element are present with the same value
in the original element.
To set or replace existing attributes on an element you have to list the elements in the value of the
attrs attribute in the xmlns="http://www.kuka.com/sunrise/xml/merge" namespace. The value must be a
comma-delimited list of name=value pairs, with no whitespace or quotes
("\", "," and "=" must be escaped as "\\", "\," and "\=" inside an attribute value, respectively). For example:
<myelement m:attrs="name=James Bond,occupation=Spy"/>
will result in attributes
<myelement name="James Bond" occupation="Spy"/>
Original document to merge:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<TestXML>
<Knoten attrib="1" value="on"/>
<Knoten attrib="2" value="on">
<Element id="1"/>
<Element id="2"/>
</Knoten>
<Knoten attrib="3" value="on">
<Element id="1"/>
<Element id="2"/>
</Knoten>
<Knoten other="other">
<Element id="1"/>
<Element id="5"/>
</Knoten>
</TestXML>
Template document to be merged into the first document:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<TestXML xmlns:m="http://www.kuka.com/sunrise/xml/merge">
<Knoten attrib="4" value="on"/>
<Knoten>
<Element id="3"/>
</Knoten>
<Knoten attrib="5" value="on"/>
<Knoten attrib="2" m:attrs="value=off"/>
<Knoten attrib="3">
<Element id="2" m:attrs="id=-1,comment=Hello World"/>
</Knoten>
</TestXML>
Resulting document:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<TestXML>
<Knoten attrib="1" value="on">
<Element id="3"/>
</Knoten>
<Knoten attrib="2" value="off">
<Element id="1"/>
<Element id="2"/>
<Element id="3"/>
</Knoten>
<Knoten attrib="3" value="on">
<Element id="1"/>
<Element id="-1" comment="Hello World"/>
<Element id="3"/>
</Knoten>
<Knoten other="other">
<Element id="1"/>
<Element id="5"/>
<Element id="3"/>
</Knoten>
<Knoten attrib="4" value="on"/>
<Knoten attrib="5" value="on"/>
</TestXML>
original - the original document where new XML element should be addedtemplate - the document with the new XML elements that should be merged into the original document. The document
must have been parsed with XML namespace support.
public static Document replaceVariablesAndParse(URL url)
url - The url to load.
public static boolean hasAttribute(String name,
Node node)
name - The name of the attribute.node - The node.
public static String requiredAttribute(String name,
Node node)
name - The name of the attribute.node - The node from which we read out the attribute.
public static String optionalAttribute(String name,
Node node,
String defaultValue)
name - The name of the attribute.node - The node from which we read out the attribute.defaultValue - The default value.
public static Node getChildNode(String childName,
Node node)
childName - The child node name.node - The parent node.
public static Node getOptionalChildNode(String childName,
Node node)
childName - The child node name.node - The parent node.
public static Element getSingleElement(Element parent,
String elementName)
parent - the parent to searchelementName - name of the Tag
public static ParameterSet createParameterSetFromNode(Node node)
node - The node.
public static void createAttribute(Node node,
String name,
String value)
node - The node.name - The name of the attribute.value - The value of the attribute.public static void removeIgnorableWhitespace(Document doc)
doc - the document which will recursively be stripped of whitespacepublic static void removeIgnorableWhitespace(Element e)
e - the element which will recursively be stripped of whitespace
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||