Package de.mhus.lib.core
Class MXml
- java.lang.Object
-
- de.mhus.lib.core.MXml
-
public class MXml extends Object
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classMXml.ElementIteratorstatic classMXml.NodeIteratorstatic interfaceMXml.ValueListener
-
Constructor Summary
Constructors Constructor Description MXml()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidcarveOut(Element element)Remove the element from his parent but append the children instead if it into the parent node - on the same position.static DocumentcreateDocument()Create and return a empty xml document.static Stringdecode(String _in)Decodes a string with encoded characters to a java string.static voiddump(PrintStream o, Node element)Prints information - most technical - of the xml element and its childs to the stream.static voiddump(PrintStream out, NodeList nodes)static voiddump(PrintStream o, Node node, String level)static Stringdump(Node element)Prints information - most technical - of the xml element and its childs and returns it as string.static Stringencode(String _in)Encode the default problematic characters in a string to store it in a xml value.static CDATASectionfindCDataSection(Element a)static StringgetAttributeValue(Element root, String path, String def)static ElementgetElementByPath(Element root, String path)Returns the first found element by path.static voidgetElementsByPath(Element root, String path, List<Element> list)Returns the first found element by path.static MXml.ElementIteratorgetLocalElementIterator(Element root)Returns an iterator of all elements in the given root element on the first level only.static MXml.ElementIteratorgetLocalElementIterator(Element root, String name)Returns an iterator of elements with this name in the given root element on the first level only.static NodeListgetLocalElements(Element root)Returns a list of all elements in the given root element on the first level only.static NodeListgetLocalElements(Element root, String name)Returns a list of elements with this name in the given root element on the first level only.static StringgetPathAsString(Node in)Returns the path to the node as string representation, separated with slashes.static StringgetValue(Element root, boolean inner)Returns the text value of a node.static StringgetValue(Element root, MXml.ValueListener listener)Returns the text value of a node.static StringgetValue(Element root, String path, String def)static StringinnerXml(Node node)Returns the inner XML Structure as string with all tag definitions.static StringinnerXml(Node node, boolean instructions)Returns the inner XML Structure as string with all tag definitions.static DocumentloadXml(File f)static DocumentloadXml(InputStream is)Create a XML Document from a stream resource.static DocumentloadXml(Reader file)static DocumentloadXml(String xml)Create a XML Document from a string.static DocumentloadXml(String xml, String charset)Create a XML Document from a string.static DocumentBuildernewBuilder()static StringnormalizeName(String key)Normalize the name of attributes or node names.static MXml.NodeIteratorqueryXPath(Node root, String query)Execute an XPATH query for a list of nodes.static MXml.ElementIteratorqueryXPathElements(Node root, String query)Execute an XPATH query for a list of elements.static StringremoveHtmlTags(String txt)static StringremoveTags(String txt)static voidsaveXml(Node e, File out)static voidsaveXml(Node e, OutputStream out)Write the element into the stream.static voidsaveXml(Node e, Writer out, boolean intend)static StringtoString(Node e, boolean intend)static voidtrim(Element element)Remove white spaces in the text nodes.static StringunicodeEncode(String _in)Encodes the amp and all characters greater then 255 to unicode representation with amp and hash signs.
-
-
-
Method Detail
-
getValue
public static String getValue(Element root, boolean inner)
Returns the text value of a node.- Parameters:
root- The element where you need the text value frominner- If true all inner elements are parsed and the text appended- Returns:
- null if root is null or the text of the element
-
getValue
public static String getValue(Element root, MXml.ValueListener listener)
Returns the text value of a node. The listener manipulates the output for different inner notes.- Parameters:
root-listener-- Returns:
- null if the root is null or the text
-
getLocalElements
public static NodeList getLocalElements(Element root, String name)
Returns a list of elements with this name in the given root element on the first level only.- Parameters:
root-name-- Returns:
- null if the root or name is null else the list of nodes
-
getLocalElements
public static NodeList getLocalElements(Element root)
Returns a list of all elements in the given root element on the first level only.- Parameters:
root-- Returns:
- null if the root is null otherwise the list of nodes
-
getLocalElementIterator
public static MXml.ElementIterator getLocalElementIterator(Element root, String name)
Returns an iterator of elements with this name in the given root element on the first level only.- Parameters:
root-name-- Returns:
- every time an iterator - never null
-
getLocalElementIterator
public static MXml.ElementIterator getLocalElementIterator(Element root)
Returns an iterator of all elements in the given root element on the first level only.- Parameters:
root-- Returns:
- iterator of the requested elements
-
getElementByPath
public static Element getElementByPath(Element root, String path)
Returns the first found element by path. Available search definitions are slash and brackets, e.g. "/root/somenode/number[3]/name" or "/root/somenode/filter@key=value/name"- Parameters:
root-path-- Returns:
- null if root or path is null or the element is not found
-
getElementsByPath
public static void getElementsByPath(Element root, String path, List<Element> list)
Returns the first found element by path. Available search definitions are slash and brackets, e.g. "/root/somenode/number[3]/name" or "/root/somenode/filter@key=value/name" Add an amp before a node to collect all the nodes between too, e.g. "/root/somenode/&filter@key=value/name"- Parameters:
root- Start elementpath- Path to golist- Result list (findings will be added)
-
getPathAsString
public static String getPathAsString(Node in)
Returns the path to the node as string representation, separated with slashes.- Parameters:
in-- Returns:
- an empty string if the node is null
-
loadXml
public static Document loadXml(String xml, String charset) throws ParserConfigurationException, UnsupportedEncodingException, SAXException, IOException
Create a XML Document from a string. Using the defined charset.- Parameters:
xml-charset-- Returns:
- the document of the xml model
- Throws:
ParserConfigurationExceptionUnsupportedEncodingExceptionSAXExceptionIOException
-
newBuilder
public static DocumentBuilder newBuilder() throws ParserConfigurationException
- Throws:
ParserConfigurationException
-
loadXml
public static Document loadXml(String xml) throws ParserConfigurationException, SAXException, IOException
Create a XML Document from a string.- Parameters:
xml-- Returns:
- the xml model
- Throws:
ParserConfigurationExceptionSAXExceptionIOException
-
loadXml
public static Document loadXml(InputStream is) throws ParserConfigurationException, SAXException, IOException
Create a XML Document from a stream resource.- Parameters:
is-- Returns:
- the xml model
- Throws:
ParserConfigurationExceptionSAXExceptionIOException
-
loadXml
public static Document loadXml(Reader file) throws ParserConfigurationException, SAXException, IOException
-
loadXml
public static Document loadXml(File f) throws ParserConfigurationException, SAXException, IOException
-
saveXml
public static void saveXml(Node e, OutputStream out) throws Exception
Write the element into the stream.- Parameters:
e-out-- Throws:
Exception
-
saveXml
public static void saveXml(Node e, Writer out, boolean intend) throws Exception
- Throws:
Exception
-
createDocument
public static Document createDocument() throws Exception
Create and return a empty xml document.- Returns:
- xml model
- Throws:
Exception
-
encode
public static String encode(String _in)
Encode the default problematic characters in a string to store it in a xml value.- Parameters:
_in-- Returns:
- encoded string
-
unicodeEncode
public static String unicodeEncode(String _in)
Encodes the amp and all characters greater then 255 to unicode representation with amp and hash signs.- Parameters:
_in-- Returns:
- encoded string
-
decode
public static String decode(String _in)
Decodes a string with encoded characters to a java string.- Parameters:
_in-- Returns:
- decoded string
-
dump
public static String dump(Node element)
Prints information - most technical - of the xml element and its childs and returns it as string. Use this function for debugging (debugger).- Parameters:
element-- Returns:
- a dump representation
-
dump
public static void dump(PrintStream o, Node element)
Prints information - most technical - of the xml element and its childs to the stream.- Parameters:
o-element-
-
dump
public static void dump(PrintStream o, Node node, String level)
-
dump
public static void dump(PrintStream out, NodeList nodes)
-
innerXml
public static String innerXml(Node node)
Returns the inner XML Structure as string with all tag definitions.- Parameters:
node-- Returns:
- inner structure as string
-
innerXml
public static String innerXml(Node node, boolean instructions)
Returns the inner XML Structure as string with all tag definitions.- Parameters:
node-instructions- set to false to ignore processing instructions (on the first level)- Returns:
- null if the node is null otherwise the innerXml as text
-
queryXPath
public static MXml.NodeIterator queryXPath(Node root, String query) throws XPathExpressionException
Execute an XPATH query for a list of nodes.- Parameters:
root-query-- Returns:
- never null
- Throws:
XPathExpressionException
-
queryXPathElements
public static MXml.ElementIterator queryXPathElements(Node root, String query) throws XPathExpressionException
Execute an XPATH query for a list of elements.- Parameters:
root-query-- Returns:
- never null
- Throws:
XPathExpressionException
-
trim
public static void trim(Element element)
Remove white spaces in the text nodes.- Parameters:
element-
-
carveOut
public static void carveOut(Element element)
Remove the element from his parent but append the children instead if it into the parent node - on the same position.- Parameters:
element-
-
normalizeName
public static String normalizeName(String key)
Normalize the name of attributes or node names. Allowed characters are (a-z, A-Z, 0-9, - _ .) all other characters are replaced with _- Parameters:
key-- Returns:
- normalized key or null if the key is null.
-
findCDataSection
public static CDATASection findCDataSection(Element a)
-
-