Class XmlNodeBuilder
XmlNodeImpl DOM tree from XML input using an XmlPullParser.
All build methods in this class parse a single XML document element (including
its children) from the input and return it as an XmlNodeImpl. The caller is
responsible for closing any InputStream or Reader passed to these methods.
Multi-document stream reading
When the underlying Reader or InputStream contains multiple concatenated
XML documents, each call to build consumes exactly one root element and its children.
The stream position is left immediately after the closing tag of that element, so a subsequent
call to build with a new parser wrapping the same reader will parse the next document.
For example, given a reader over the concatenation of two identical documents:
String doc = "<?xml version='1.0'?><doc><child>foo</child></doc>"; Reader r = new StringReader(doc + doc); XmlNode first = XmlService.read(r); // reads the first <doc> XmlNode second = XmlService.read(r); // reads the second <doc> // first.equals(second) is true
Whitespace trimming
By default, text content is trimmed of leading and trailing whitespace. This can be
disabled by passing trim = false, or on a per-element basis by setting the
xml:space="preserve" attribute on an element.
Empty vs. self-closing elements
Self-closing tags (e.g. <item/>) produce a node whose value is null.
An element with an explicit open and close tag but no content (e.g. <item></item>)
produces a node whose value is the empty string "".
Child elements vs. text content
If an element contains child elements, the resulting node carries the children and its
text value is null, even if there is interleaved text content. If an element
contains only text (no child elements), the node carries the text value and has no
children.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceDeprecated.Callback interface for creating input location objects during parsing. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic XmlNodeImplbuild(InputStream is, String encoding) Deprecated.Builds an XML node tree from the given input stream, trimming whitespace by default.static XmlNodeImplbuild(InputStream is, String encoding, boolean trim) Deprecated.Builds an XML node tree from the given input stream.static XmlNodeImplDeprecated.Builds an XML node tree from the given reader, trimming whitespace by default and without tracking input locations.static XmlNodeImplDeprecated.Builds an XML node tree from the given reader without location tracking.static XmlNodeImplbuild(Reader reader, boolean trim, XmlNodeBuilder.InputLocationBuilder locationBuilder) Deprecated.Builds an XML node tree from the given reader.static XmlNodeImplbuild(Reader reader, XmlNodeBuilder.InputLocationBuilder locationBuilder) Deprecated.Builds an XML node tree from the given reader, trimming whitespace by default.static XmlNodeImplbuild(org.codehaus.plexus.util.xml.pull.XmlPullParser parser) Deprecated.Builds an XML node tree from the given pull parser, trimming whitespace by default and without tracking input locations.static XmlNodeImplbuild(org.codehaus.plexus.util.xml.pull.XmlPullParser parser, boolean trim) Deprecated.Builds an XML node tree from the given pull parser without location tracking.static XmlNodeImplbuild(org.codehaus.plexus.util.xml.pull.XmlPullParser parser, boolean trim, XmlNodeBuilder.InputLocationBuilder locationBuilder) Deprecated.Core parsing method.
-
Constructor Details
-
XmlNodeBuilder
public XmlNodeBuilder()Deprecated.
-
-
Method Details
-
build
public static XmlNodeImpl build(Reader reader) throws org.codehaus.plexus.util.xml.pull.XmlPullParserException, IOException Deprecated.Builds an XML node tree from the given reader, trimming whitespace by default and without tracking input locations.- Parameters:
reader- the reader to parse XML from- Returns:
- the parsed XML node tree
- Throws:
org.codehaus.plexus.util.xml.pull.XmlPullParserException- if the XML is not well-formedIOException- if an I/O error occurs while reading
-
build
public static XmlNodeImpl build(Reader reader, XmlNodeBuilder.InputLocationBuilder locationBuilder) throws org.codehaus.plexus.util.xml.pull.XmlPullParserException, IOException Deprecated.Builds an XML node tree from the given reader, trimming whitespace by default.- Parameters:
reader- the reader to parse XML fromlocationBuilder- optional builder for recording input locations of parsed elements, ornullto skip location tracking- Returns:
- the parsed XML node tree
- Throws:
org.codehaus.plexus.util.xml.pull.XmlPullParserException- if the XML is not well-formedIOException- if an I/O error occurs while reading- Since:
- 3.2.0
-
build
public static XmlNodeImpl build(InputStream is, String encoding) throws org.codehaus.plexus.util.xml.pull.XmlPullParserException, IOException Deprecated.Builds an XML node tree from the given input stream, trimming whitespace by default.- Parameters:
is- the input stream to parse XML fromencoding- the character encoding of the stream (e.g."UTF-8"), ornullto let the parser detect it- Returns:
- the parsed XML node tree
- Throws:
org.codehaus.plexus.util.xml.pull.XmlPullParserException- if the XML is not well-formedIOException- if an I/O error occurs while reading
-
build
public static XmlNodeImpl build(InputStream is, String encoding, boolean trim) throws org.codehaus.plexus.util.xml.pull.XmlPullParserException, IOException Deprecated.Builds an XML node tree from the given input stream.- Parameters:
is- the input stream to parse XML fromencoding- the character encoding of the stream (e.g."UTF-8"), ornullto let the parser detect ittrim- iftrue, leading and trailing whitespace is removed from text content unless the element hasxml:space="preserve"- Returns:
- the parsed XML node tree
- Throws:
org.codehaus.plexus.util.xml.pull.XmlPullParserException- if the XML is not well-formedIOException- if an I/O error occurs while reading
-
build
public static XmlNodeImpl build(Reader reader, boolean trim) throws org.codehaus.plexus.util.xml.pull.XmlPullParserException, IOException Deprecated.Builds an XML node tree from the given reader without location tracking.- Parameters:
reader- the reader to parse XML fromtrim- iftrue, leading and trailing whitespace is removed from text content unless the element hasxml:space="preserve"- Returns:
- the parsed XML node tree
- Throws:
org.codehaus.plexus.util.xml.pull.XmlPullParserException- if the XML is not well-formedIOException- if an I/O error occurs while reading
-
build
public static XmlNodeImpl build(Reader reader, boolean trim, XmlNodeBuilder.InputLocationBuilder locationBuilder) throws org.codehaus.plexus.util.xml.pull.XmlPullParserException, IOException Deprecated.Builds an XML node tree from the given reader.- Parameters:
reader- the reader to parse XML fromtrim- iftrue, leading and trailing whitespace is removed from text content unless the element hasxml:space="preserve"locationBuilder- optional builder for recording input locations of parsed elements, ornullto skip location tracking- Returns:
- the parsed XML node tree
- Throws:
org.codehaus.plexus.util.xml.pull.XmlPullParserException- if the XML is not well-formedIOException- if an I/O error occurs while reading- Since:
- 3.2.0
-
build
public static XmlNodeImpl build(org.codehaus.plexus.util.xml.pull.XmlPullParser parser) throws org.codehaus.plexus.util.xml.pull.XmlPullParserException, IOException Deprecated.Builds an XML node tree from the given pull parser, trimming whitespace by default and without tracking input locations.- Parameters:
parser- the pull parser positioned at or before the root element's start tag- Returns:
- the parsed XML node tree
- Throws:
org.codehaus.plexus.util.xml.pull.XmlPullParserException- if the XML is not well-formedIOException- if an I/O error occurs while reading
-
build
public static XmlNodeImpl build(org.codehaus.plexus.util.xml.pull.XmlPullParser parser, boolean trim) throws org.codehaus.plexus.util.xml.pull.XmlPullParserException, IOException Deprecated.Builds an XML node tree from the given pull parser without location tracking.- Parameters:
parser- the pull parser positioned at or before the root element's start tagtrim- iftrue, leading and trailing whitespace is removed from text content unless the element hasxml:space="preserve"- Returns:
- the parsed XML node tree
- Throws:
org.codehaus.plexus.util.xml.pull.XmlPullParserException- if the XML is not well-formedIOException- if an I/O error occurs while reading
-
build
public static XmlNodeImpl build(org.codehaus.plexus.util.xml.pull.XmlPullParser parser, boolean trim, XmlNodeBuilder.InputLocationBuilder locationBuilder) throws org.codehaus.plexus.util.xml.pull.XmlPullParserException, IOException Deprecated.Core parsing method. Builds an XML node tree from the given pull parser.Parsing begins at the parser's current event and consumes tokens through the matching end tag of the first start tag encountered. When this method returns, the parser is positioned immediately after that end tag, so the caller (or a subsequent
buildcall) can continue reading the same stream.Child elements are parsed recursively. If the element contains only text content (no child elements), the text is stored as the node's value. If child elements are present, the text value is
nulland children are accessible viaXmlNodeImpl.getChildren().- Parameters:
parser- the pull parser positioned at or before the root element's start tagtrim- iftrue, leading and trailing whitespace is removed from text content unless the element hasxml:space="preserve"locationBuilder- optional builder for recording input locations of parsed elements, ornullto skip location tracking- Returns:
- the parsed XML node tree
- Throws:
org.codehaus.plexus.util.xml.pull.XmlPullParserException- if the XML is not well-formedIOException- if an I/O error occurs while readingIllegalStateException- if the end of the document is reached before the root element's end tag is found- Since:
- 3.2.0
-
XmlServiceinstead.