public class XmlExtensions extends java.lang.Object
getChildren(Element, String)
, getTextFromPath(Element, String, boolean)
and setTextToPath(Element, String, String )
. With this methods a textual controlled access
to any content inside Elements is possible, without calling a XSL-translator, but directly.
The textual given pathes are comparably to XPATH, but not equivalent.xslTransformString(Element, File)
and xslTransformXml(Element, File)
.
The cause of this wrappers are catch of exceptions and supply detached xml elements (not bind to a document)
as result of XSL translation.replaceWhiteSpaceWith1Space(Element, boolean)
implements this feature. The reduced input xml tree may be processed
by any XSL translation or other XML processing in Java. The output may be beautificated for better readability by human.
But the spaces of text content may be beware. The beautification is only made respecitvely to the superior organisation elements.
The methods beautificationBewareTextContent(Element)
and the writeXmlBeautificatedTextFile(Element, File)
do so.
create(Charset)
,
the constructors are private. This is a design attempt ajar to factory patterns.
The using of new XmlExtension(...)
instead create(...)
will be
adequate, but here create() is the decision.
date who change 2007-01-02 JcHartmut create() with Charset, some methods will be non static. 2006-03-07 JcHartmut beautification, replaceWhiteSpaceWith1Space. 2005-06-00 JcHartmut initial revision
Modifier and Type | Class and Description |
---|---|
static class |
XmlExtensions.XmlMode
Class hold some properties of transformation.
|
Constructor and Description |
---|
XmlExtensions() |
Modifier and Type | Method and Description |
---|---|
static Element |
beautificationBewareTextContent(Element xml)
Beautificates the content of the Element with respecting of textual content preserving.
|
static java.util.List<org.jdom.Parent> |
getChildren(Element xml,
java.lang.String sXmlPath)
Gets a list of children, not only from the given xml Element, but also from a deeper level.
|
static java.lang.String |
getTextFromPath(Element xml,
java.lang.String sXmlPath,
boolean bInvalidIsOk)
Gets the text content from the adressed node relativ to the given Element.
|
static Element |
readXmlFile(java.io.File file)
Reads a xml file and convert it to a internal xml tree.
|
static Element |
readXmlFileTrimWhiteSpace(java.io.File file)
Reads a xml file and convert it to a internal xml tree.
|
static void |
replaceWhiteSpaceWith1Space(Element xml,
boolean bContendText)
Replaces white spaces of all text()-content with one space, by keeping
the inner leading and trailing spaces.
|
static void |
reportContentElement(Element xml,
Report report)
Write the content of the xml-node in the report
|
static boolean |
setTextToPath(Element xml,
java.lang.String sXmlPath,
java.lang.String sContent)
Sets the specified element with the given value.
|
static void |
writeXmlBeautificatedTextFile(Element xmlRoot,
java.io.File fileOut,
java.nio.charset.Charset encoding)
Output a xml tree to a file with beautification of the output,
but beware all spaces inside textual content.
|
static void |
writeXmlDirect(Element xml,
java.io.File fileOut,
java.lang.String sEncoding)
Output a xml tree to a file.
|
static void |
writeXmlDirect(Element xml,
java.io.Writer out,
java.lang.String sEncoding)
Output a xml tree to a file.
|
void |
writeXmlFile(Element xmlRoot,
java.io.File fileOut)
Output a xml tree to a file.
|
static void |
writeXmlFile(Element xmlRoot,
java.io.File fileOut,
XmlExtensions.XmlMode mode)
Output a xml tree to a file.
|
static java.lang.String |
xslTransformString(Element xmlInput,
java.io.File xslFile)
Transform a xml tree to a string.
|
static Element |
xslTransformXml(Element xmlInput,
java.io.File xslFile)
XSL-Transformation of a xml tree to a new tree.
|
public XmlExtensions()
public static Element beautificationBewareTextContent(Element xml)
<sampleTag><innerTag><p>This is text<b> with bold </b>and i<b>nn</b>er bold text</p></innerTag></sampleTag>The output will be:
<sampleTag> <innerTag> <p>This is text<b> with bold </b>and i<b>nn</b>er bold text</p> </innerTag> </sampleTag>
<sampleTag> <innerTag> <p>This is text <b> with bold </b> and i <b>nn </b> er bold text </p> </innerTag> </sampleTag>In this sample always a whitespace is produced between elements in output, even if the user will not have a space.
xml
- The Input element tree.public static java.util.List<org.jdom.Parent> getChildren(Element xml, java.lang.String sXmlPath)
"element1/name"
: children of element1 with tag name "name""element1/*"
: all children of element1"../name"
: all siblings with name"../*"
: all siblings inclusive self".."
: Only the parent of self"../.."
: Only the parent of parent"."
: Only self (it is the param xml itself)".."
or "."
, the list contains only the addressed element,
it is not a list getted with org.jdom.Element.getChildren()
, it is a simple LinkedList
with this one element.
org.jdom.Element.getChildren()
.
It means, that all operations may be applied to the list, including add of further siblings
using thelist.listIterator().add(sibling);
xml
- The parent element.sXmlPath
- Path relativ from parent to the element from which the children are listed.
If the path is "." or empty,public static java.lang.String getTextFromPath(Element xml, java.lang.String sXmlPath, boolean bInvalidIsOk)
sample | meaning |
---|---|
name | Normalized text content of the element |
@name | content of the attribute |
name/child | Normalized text content of the child element |
name/@attr | content of the attribute within the element |
name/name/@attr | The path can be in any deepness |
../../name | parent from parent |
"text" |
the constant text between "". It may be a part of the path concating with other parts with +, but it is also possible to return a text directly, without using the input xml element. This choice is conveniently, because the behaviour may be determined outside of a java programming, and no special case will be produced if only a simple text is expected. |
path+"text"+path | concation between the texts from pathes |
path+c+path | 1 char between + is a added separator, it is the same as +"c"+ |
path+c | 1 char on end after +, same as here +"." |
{apath|vpath|c} |
concation of all textes from vpath from all elements found with apath ,
with c as separator between the concatenated result strings. With using this expression as part of sXmlPath it is possible to concatenate the content of more as one elements without using of a extra programmed loop. |
xml
- The Element the path starts from.sXmlPath
- The path expression see above.bInvalidIsOk
- If true, than return "" on invalid path, if false than return null on invalid path.public static Element readXmlFile(java.io.File file) throws XmlException
XmlException
public static Element readXmlFileTrimWhiteSpace(java.io.File file) throws XmlException
replaceWhiteSpaceWith1Space(Element, boolean)
.file
- The file from which xml is readed.XmlException
public static void replaceWhiteSpaceWith1Space(Element xml, boolean bContendText)
xml
- The element to be white-spaces-trimmed.bContendText
- Normally let it false by calling outside!
If false, than test of containing text(), if it contains text() than
delete the first leading and the last trailing whitespace
and call recursively for inner elements with true.public static void reportContentElement(Element xml, Report report)
public static boolean setTextToPath(Element xml, java.lang.String sXmlPath, java.lang.String sContent)
xml
- Element within the content is settedsXmlPath
- Path selects the child within xml, into the sContent is setted.
The path should be given in form "../../name/name/dst".
The specification ".." means the parent, like XPATH.
All childs are created, if they don't exist.form | Description |
---|---|
name | the child with the name |
. | The element itself (sXmlPath="." is the input element itself) |
@name | example: "tag/@name". Sets the content into the attribute name of the tag |
= | example: "tag/tag2/=". Sets the content into the element tag2, otherwise the content is appended to name2 |
! | example: "tag/tag2/!". Changes the name of the selected xml-Element tag2 to the sContent |
sContent
- content set to the element, attribute or defines the name of the element.public static void writeXmlBeautificatedTextFile(Element xmlRoot, java.io.File fileOut, java.nio.charset.Charset encoding) throws XmlException, java.io.FileNotFoundException
beautificationNoTextContent(Element)
xmlRoot
- The root elementfileOut
- The file to write out. The file will be created or replaced.java.io.FileNotFoundException
- If the fileOut doesn't match.XmlException
public static void writeXmlDirect(Element xml, java.io.File fileOut, java.lang.String sEncoding) throws XmlException, java.io.FileNotFoundException
xmlRoot
- The root Element of the created output XML-File.fileOut
- This file will be created or replaced.sEncoding
- The encoding, typicall "ISO-8859-1"java.io.FileNotFoundException
- if the file exists but is a directory
rather than a regular file, does not exist but cannot
be created, or cannot be opened for any other reasonjava.io.IOException
- if any error on writing at file systemjava.lang.SecurityException
- if a security manager exists and its
checkWrite
method denies write access
to the file.XmlException
public static void writeXmlDirect(Element xml, java.io.Writer out, java.lang.String sEncoding) throws XmlException, java.io.IOException
xmlRoot
- The root Element of the created output XML-File.fileOut
- This file will be created or replaced.sEncoding
- The encoding, typicall "ISO-8859-1"java.io.FileNotFoundException
- if the file exists but is a directory
rather than a regular file, does not exist but cannot
be created, or cannot be opened for any other reasonjava.io.IOException
- if any error on writing at file systemjava.lang.SecurityException
- if a security manager exists and its
checkWrite
method denies write access
to the file.XmlException
public void writeXmlFile(Element xmlRoot, java.io.File fileOut) throws XmlException, java.io.FileNotFoundException
create(Charset)
or setEncoding(int)
.xmlRoot
- The root Element of the created output XML-File.fileOut
- This file will be created or replaced.java.io.FileNotFoundException
- if the file exists but is a directory
rather than a regular file, does not exist but cannot
be created, or cannot be opened for any other reasonjava.io.IOException
- if any error on writing at file systemjava.lang.SecurityException
- if a security manager exists and its
checkWrite
method denies write access
to the file.XmlException
public static void writeXmlFile(Element xmlRoot, java.io.File fileOut, XmlExtensions.XmlMode mode) throws XmlException, java.io.FileNotFoundException
xmlRoot
- The root Element of the created output XML-File.fileOut
- This file will be created or replaced.sEncoding
- The encoding, typicall "ISO-8859-1"java.io.FileNotFoundException
- if the file exists but is a directory
rather than a regular file, does not exist but cannot
be created, or cannot be opened for any other reasonjava.io.IOException
- if any error on writing at file systemjava.lang.SecurityException
- if a security manager exists and its
checkWrite
method denies write access
to the file.XmlException
public static java.lang.String xslTransformString(Element xmlInput, java.io.File xslFile) throws XmlException
XmlException
public static Element xslTransformXml(Element xmlInput, java.io.File xslFile) throws XmlException
XmlException