Class XmlReader
java.lang.Object
com.northconcepts.datapipeline.core.DataObject
com.northconcepts.datapipeline.core.Endpoint
com.northconcepts.datapipeline.core.DataEndpoint
com.northconcepts.datapipeline.core.DataReader
com.northconcepts.datapipeline.xml.XmlReader
- Direct Known Subclasses:
JavaBeanReader,JsonReader
Obtains records from an XML stream. See the Read an XML file example.
XmlReader's addField(String, String), addField(String, String, boolean), and addRecordBreak(String) methods use a subset of the
XPath 1.0 location paths notation to identify field values and demarcate records.
Axis Specifiers
Axis Abbreviated Syntax Supported Examples ancestor ancestor-or-self attribute @ yes @lang or attribute::lang child yes title or child::title descendant yes descendant-or-self // yes //book or /descendant-or-self::book/ following following-sibling namespace parent .. preceding preceding-sibling self . yes
Node Tests
- comment(), text(), processing-instruction(), node() are all supported
Predicates
- None supported
Functions and Operators
- None supported
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumStrategy for a field whose location path matches more than once within the same record.Nested classes/interfaces inherited from class com.northconcepts.datapipeline.core.DataEndpoint
DataEndpoint.State -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected Recordprotected final Fileprotected booleanprotected final XmlNodeReaderprotected final List<XmlRecordBreak> Fields inherited from class com.northconcepts.datapipeline.core.DataReader
fieldLineage, recordLineageFields inherited from class com.northconcepts.datapipeline.core.DataEndpoint
lastRecord, PRODUCT, PRODUCT_VERSION, VENDOR, XML_INPUT_FACTORY_KEYFields inherited from class com.northconcepts.datapipeline.core.Endpoint
BUFFER_SIZE, captureElapsedTime, DEFAULT_READ_BUFFER_SIZEFields inherited from class com.northconcepts.datapipeline.core.DataObject
id, log, name, TIMESTAMP_FORMAT -
Constructor Summary
ConstructorsConstructorDescriptionXmlReader(XMLStreamReader streamReader) Creates a reader for an existing StAX stream; closing this reader closes the stream reader but not its underlying input.XmlReader(XmlNodeReader reader) Creates a reader over the given node source; used by subclasses that present other formats, such as JSON or Java beans, as XML nodes. -
Method Summary
Modifier and TypeMethodDescriptionaddExceptionProperties(DataException exception) Adds this endpoint's current state to aDataException.Identifies a new field using XPath in the XML stream.Identifies a new field using XPath in the XML stream.Identifies a new field using XPath in the XML stream.protected voidaddFieldValue(Field recordField, Object value) protected RecordaddLineage(Record record) Called byDataReader.read()for each record fromDataReader.readImpl()while lineage is saved; the default copiesrecordLineageinto every field along with its original index and name.addRecordBreak(String locationPathAsString) Adds a location path (for example//book); a record is completed each time a matching element ends.voidclose()Indicates that this endpoint has finished reading or writing.protected voidStarts a new current record with one field per addedXmlField, copying the values of cascading fields from the previous record.protected voidexpandAndPushRecords(Record record) protected voidexpandListFieldsAsFields(Record record) Returns how a field matched more than once in a record is handled (default isXmlReader.DuplicateFieldPolicy.USE_LAST_VALUE).protected voidgetFieldValues(long sequence) protected XmlNodeReaderbooleanReturn true if each child node's text should be concatenated to its parent during parsing (defaults to false).booleanIndicates if theReaderpassed to the constructor is closed when this reader closes (default is true); a file opened by this reader is always closed.booleanisDebug()Indicates if each node read is logged at debug level (default is false).booleanIndicates if namespaces on elements and attributes are ignored when matching expressions (default to true).booleanIndicates if this reader can capture record and field lineage (false unless overridden by a reader that supports it).protected booleanisRecordBreak(XmlNode node) Returns true if the node matches any location path added withaddRecordBreak(String).voidopen()Makes this endpoint ready for reading or writing.protected RecordreadImpl()Overridden by subclasses to read the next record from thisDataReader.protected voidsaveAncestorAttributeFieldValues(XmlNode node) Sets the attribute fields (paths ending in an attribute step) matching the record-break node or an ancestor.protected voidsaveAncestorNodeFieldValues(XmlNode node) Fills the still-empty element fields from ancestors of the record-break node that match their location paths.protected voidsaveFieldValues(XmlNode node) Sets the fields whose location paths match the node and clears cascading fields whose reset path matches it.setAddTextToParent(boolean addTextToParent) Indicates if each child node's text should be concatenated to its parent during parsing (defaults to false).setAutoCloseReader(boolean autoCloseReader) Indicates if theReaderpassed to the constructor is closed when this reader closes (default is true); a file opened by this reader is always closed.setDebug(boolean debug) Indicates if each node read is logged at debug level (default is false).setDescription(String description) Sets the optional text shown for this endpoint inEndpoint.toString()and exception properties.setDuplicateFieldPolicy(XmlReader.DuplicateFieldPolicy duplicateFieldPolicy) Sets how a field matched more than once in a record is handled; null restores the defaultXmlReader.DuplicateFieldPolicy.USE_LAST_VALUE.setIgnoreNamespaces(boolean ignoreNamespaces) Indicates if namespaces on elements and attributes are ignored when matching expressions (default to true).protected voidsetRecordField(XmlNode node, XmlField xmlField, LocationPath path) Stores the valuepathselects from the node in the current record's field, replacing the field's value underXmlReader.DuplicateFieldPolicy.USE_LAST_VALUEand adding to it otherwise.setSaveLineage(boolean saveLineage) Indicates if record and field lineage is captured for each record read (default is false); enabling it throws ifDataReader.isLineageSupported()is false or the product edition does not include lineage.Methods inherited from class com.northconcepts.datapipeline.core.DataReader
available, getBufferSize, getNestedEndpoint, getNestedReader, getReader, getRootEndpoint, getRootReader, isExhausted, isSaveLineage, peek, pop, push, read, skipMethods inherited from class com.northconcepts.datapipeline.core.DataEndpoint
decrementRecordCount, enableJmx, getLastRecord, getRecordCount, getRecordCountAsBigInteger, getRecordCountAsString, incrementRecordCount, isRecordCountBigInteger, resetRecordCount, toStringMethods inherited from class com.northconcepts.datapipeline.core.Endpoint
addElapsedtime, assertClosed, assertNotOpened, assertOpened, finalize, getClosedOn, getDescription, getElapsedTime, getElapsedTimeAsString, getOpenedOn, getOpenElapsedTime, getOpenElapsedTimeAsString, getSelfTime, getSelfTimeAsString, getState, isCaptureElapsedTime, isClosed, isOpen, setCaptureElapsedTime
-
Field Details
-
recordBreaks
-
fields
-
reader
protected final XmlNodeReader reader -
file
-
currentRecord
-
hasCascadingFields
protected boolean hasCascadingFields
-
-
Constructor Details
-
XmlReader
-
XmlReader
-
XmlReader
Creates a reader for an existing StAX stream; closing this reader closes the stream reader but not its underlying input. -
XmlReader
public XmlReader(XmlNodeReader reader) Creates a reader over the given node source; used by subclasses that present other formats, such as JSON or Java beans, as XML nodes.
-
-
Method Details
-
isDebug
public boolean isDebug()Indicates if each node read is logged at debug level (default is false). -
setDebug
Indicates if each node read is logged at debug level (default is false). -
getXmlNodeReader
protected XmlNodeReader getXmlNodeReader() -
isAddTextToParent
public boolean isAddTextToParent()Return true if each child node's text should be concatenated to its parent during parsing (defaults to false). -
setAddTextToParent
Indicates if each child node's text should be concatenated to its parent during parsing (defaults to false). Setting this totruewill result in higher memory consumption. -
addRecordBreak
Adds a location path (for example//book); a record is completed each time a matching element ends. -
addField
-
addField
Identifies a new field using XPath in the XML stream.- Parameters:
name- the field name to createlocationPathAsString- the XPath to match to populate this fieldcascadeValues- indicates if this reader should return the last value seen for this field when no matches are available.
-
addField
public XmlReader addField(String name, String locationPathAsString, String cascadeResetLocationPath) Identifies a new field using XPath in the XML stream. IfcascadeResetLocationPathis not null and not empty, it will indicate when this field should be cleared, otherwise this field will return the last value seen when no new matches are available.- Parameters:
name- the field name to createlocationPathAsString- the XPath to match to populate this fieldcascadeResetLocationPath- the XPath to identify when cascading values (i.e. the last value seen for this field) should be cleared.
-
addField
Identifies a new field using XPath in the XML stream.- Parameters:
name- the field name to createlocationPathAsString- the XPath to match to populate this field
-
getDuplicateFieldPolicy
Returns how a field matched more than once in a record is handled (default isXmlReader.DuplicateFieldPolicy.USE_LAST_VALUE). -
setDuplicateFieldPolicy
Sets how a field matched more than once in a record is handled; null restores the defaultXmlReader.DuplicateFieldPolicy.USE_LAST_VALUE. -
isIgnoreNamespaces
public boolean isIgnoreNamespaces()Indicates if namespaces on elements and attributes are ignored when matching expressions (default to true). -
setIgnoreNamespaces
Indicates if namespaces on elements and attributes are ignored when matching expressions (default to true). -
createRecord
protected void createRecord()Starts a new current record with one field per addedXmlField, copying the values of cascading fields from the previous record. -
setSaveLineage
Description copied from class:DataReaderIndicates if record and field lineage is captured for each record read (default is false); enabling it throws ifDataReader.isLineageSupported()is false or the product edition does not include lineage.- Overrides:
setSaveLineagein classDataReader
-
setDescription
Description copied from class:EndpointSets the optional text shown for this endpoint inEndpoint.toString()and exception properties.- Overrides:
setDescriptionin classEndpoint
-
readImpl
Description copied from class:DataReaderOverridden by subclasses to read the next record from thisDataReader. The default implementation ofDataReader.read()now insures that this method will not be called again after it returns anull.If no record is available,
nullwill be returned.Contract for subclasses (see also
docs/authoring/DataReader.md):- Return
nullexactly once to signal end-of-stream. - Do not call this method directly —
DataReader.read()is the template method that wraps exceptions, increments the record count, and tracks lineage. - Do not call
DataEndpoint.incrementRecordCount()here;DataReader.read()already does. - Throw raw exceptions;
DataReader.read()wraps them viaexception(throwable).
- Specified by:
readImplin classDataReader- Throws:
Throwable
- Return
-
expandAndPushRecords
-
expandListFieldsAsFields
-
setRecordField
Stores the valuepathselects from the node in the current record's field, replacing the field's value underXmlReader.DuplicateFieldPolicy.USE_LAST_VALUEand adding to it otherwise. -
saveFieldValues
protected void saveFieldValues(XmlNode node) Sets the fields whose location paths match the node and clears cascading fields whose reset path matches it. -
saveAncestorAttributeFieldValues
protected void saveAncestorAttributeFieldValues(XmlNode node) Sets the attribute fields (paths ending in an attribute step) matching the record-break node or an ancestor. -
saveAncestorNodeFieldValues
protected void saveAncestorNodeFieldValues(XmlNode node) Fills the still-empty element fields from ancestors of the record-break node that match their location paths. -
addFieldValue
-
getFieldValues
protected void getFieldValues(long sequence) -
isRecordBreak
protected boolean isRecordBreak(XmlNode node) Returns true if the node matches any location path added withaddRecordBreak(String). -
open
Description copied from class:DataEndpointMakes this endpoint ready for reading or writing.- Overrides:
openin classDataEndpoint- Throws:
DataException
-
close
Description copied from class:DataEndpointIndicates that this endpoint has finished reading or writing.- Overrides:
closein classDataEndpoint- Throws:
DataException
-
isLineageSupported
public boolean isLineageSupported()Description copied from class:DataReaderIndicates if this reader can capture record and field lineage (false unless overridden by a reader that supports it).- Overrides:
isLineageSupportedin classDataReader
-
addLineage
Description copied from class:DataReaderCalled byDataReader.read()for each record fromDataReader.readImpl()while lineage is saved; the default copiesrecordLineageinto every field along with its original index and name. Overrides set their source details onrecordLineagefirst and end withsuper.addLineage(record).- Overrides:
addLineagein classDataReader
-
addExceptionProperties
Description copied from class:EndpointAdds this endpoint's current state to aDataException. Since this method is called whenever an exception is thrown, subclasses should override it to add their specific information.- Overrides:
addExceptionPropertiesin classDataReader
-
isAutoCloseReader
public boolean isAutoCloseReader()Indicates if theReaderpassed to the constructor is closed when this reader closes (default is true); a file opened by this reader is always closed. -
setAutoCloseReader
Indicates if theReaderpassed to the constructor is closed when this reader closes (default is true); a file opened by this reader is always closed.
-