Class MxParseUtils
- java.lang.Object
-
- com.prowidesoftware.swift.model.mx.MxParseUtils
-
public class MxParseUtils extends java.lang.ObjectThis class provides utility methods to parse XML content using a fast event based API (no DOM) and to extract specific information from the XML content.- Since:
- 9.1.2
-
-
Constructor Summary
Constructors Constructor Description MxParseUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static booleanelementExists(java.lang.String xml, java.lang.String localName)Checks if an element exists in the XMLstatic java.util.Optional<java.lang.String>findByPath(java.lang.String xml, java.lang.String targetPath)Finds an XML element within a document by traversing a specified tag hierarchy.static java.util.Optional<java.lang.String>findByTags(java.lang.String xml, java.lang.String... tags)Finds an XML element within a document by traversing a specified tag hierarchy with potential any element in between the specified tags.static java.util.Optional<javax.xml.stream.XMLStreamReader>findElementByPath(java.lang.String xml, java.lang.String targetPath)Deprecated.usefindByTags(String, String...)insteadstatic java.util.Optional<javax.xml.stream.XMLStreamReader>findElementByTags(java.lang.String xml, java.lang.String... tags)Deprecated.usefindByTags(String, String...)insteadstatic java.lang.StringgetBICFromDN(java.lang.String dn)Distinguished Name structure: cn=name,ou=payment,o=bank,o=swift
Example: o=spxainjj,o=swiftstatic java.util.Optional<SettlementInfo>getSettlementInfo(java.lang.String xml)Extracts settlement information from the given XML document.static java.util.Optional<MxId>identifyMessage(java.lang.String xml)Takes an XML with an MX message and detects the specific message type parsing just the namespace from the Document element.static java.lang.StringmakeXmlLenient(java.lang.String xml)This method is intended to fix some malformed XML content that is not compliant with the XML specification to enable the parsing and processing of the payload to be lenient.static java.util.List<java.lang.String>parseComments(java.lang.String xml)Parses all comments from the given XML document.static java.util.List<java.lang.String>parseCommentsContains(java.lang.String xml, java.lang.String contains)Parses comments from the given XML document that contains a specific string.static java.util.List<java.lang.String>parseCommentsStartsWith(java.lang.String xml, java.lang.String startWith)Parses comments from the given XML document that start with a specific prefix.static java.util.Optional<com.prowidesoftware.swift.model.mt.AbstractMT>parseMtFromMultiformatMessage(java.lang.String xml)Parses anAbstractMTmessage from a multi-format XML message.
-
-
-
Method Detail
-
getBICFromDN
public static java.lang.String getBICFromDN(java.lang.String dn)
Distinguished Name structure: cn=name,ou=payment,o=bank,o=swift
Example: o=spxainjj,o=swift- Parameters:
dn- the DN element content- Returns:
- returns capitalized "bank", in the example SPXAINJJ
-
identifyMessage
public static java.util.Optional<MxId> identifyMessage(java.lang.String xml)
Takes an XML with an MX message and detects the specific message type parsing just the namespace from the Document element. If the Document element is not present, or without the namespace or if the namespace url contains invalid content, then attempts to identify the message from the AppHdr. From the header tries to get the message type from the 'MsgDefIdr' that is present in the Business Application Header, and if that is not present tries to get the 'MsgName' assuming it is a legacy Application Header. If none of the above is present or cannot be parsed, then returns empty.Example of a recognizable Document element:
<Doc:Document xmlns:Doc="urn:swift:xsd:camt.003.001.04" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">The implementation is intended to be lightweight and efficient, based on
XMLStreamReader- Returns:
- id with the detected MX message type or empty if it cannot be determined.
-
makeXmlLenient
public static java.lang.String makeXmlLenient(java.lang.String xml)
This method is intended to fix some malformed XML content that is not compliant with the XML specification to enable the parsing and processing of the payload to be lenient. For the moment, current implementation will just fix invalid case in the XML declaration, if present.- Parameters:
xml- original XML content- Returns:
- modified XML content with the XML declaration fixed to be compliant with the XML specification
- Since:
- 9.3.9
-
parseComments
public static java.util.List<java.lang.String> parseComments(java.lang.String xml)
Parses all comments from the given XML document.This method uses an
XMLStreamReaderto parse the provided XML string and extract all comments present in the document. Comments are identified as XML elements of typeXMLStreamConstants.COMMENT.All extracted comments are trimmed before being added to the result list. Meaning they will not contain any leading or trailing whitespace.
- Parameters:
xml- the XML document as aStringto parse- Returns:
- a
Listof comments extracted from the XML document - Throws:
java.lang.NullPointerException- if thexmlis nulljava.lang.IllegalArgumentException- if thexmlis blank or empty- Since:
- 9.5.5
-
parseCommentsStartsWith
public static java.util.List<java.lang.String> parseCommentsStartsWith(java.lang.String xml, java.lang.String startWith)Parses comments from the given XML document that start with a specific prefix.This method uses
parseComments(String)to extract all comments from the XML, filters the comments to include only those that start with the specified prefix.- Parameters:
xml- the XML document as aStringto parsestartWith- the prefix to filter comments by, leading whitespaces are ignored- Returns:
- a
Listof filtered and cropped comments that start with the given prefix - Throws:
java.lang.NullPointerException- if thexmlis nulljava.lang.IllegalArgumentException- if thexmlis blank or empty- Since:
- 9.5.5
-
parseCommentsContains
public static java.util.List<java.lang.String> parseCommentsContains(java.lang.String xml, java.lang.String contains)Parses comments from the given XML document that contains a specific string.This method uses
parseComments(String)to extract all comments from the XML, filters the comments to include only those that contains a specific string.- Parameters:
xml- the XML document as aStringto parsecontains- the content to filter comments by- Returns:
- a
Listof filtered and cropped comments that start with the given prefix - Throws:
java.lang.NullPointerException- if thexmlis nulljava.lang.IllegalArgumentException- if thexmlis blank or empty- Since:
- 9.5.5
-
parseMtFromMultiformatMessage
public static java.util.Optional<com.prowidesoftware.swift.model.mt.AbstractMT> parseMtFromMultiformatMessage(java.lang.String xml)
Parses anAbstractMTmessage from a multi-format XML message.This method searches for MT (Message Type) content within the comments of the provided XML document. Specifically, it extracts comments that start with the prefix "{1:F0", which indicates the presence of an MT message, and attempts to parse the first matching comment into an
AbstractMTobject.If an error occurs during parsing or no matching comments are found, the method returns an empty
Optional.- Parameters:
xml- the XML document as aStringcontaining the multi-format message- Returns:
- an
Optionalcontaining the parsedAbstractMTif successful; otherwise, an emptyOptional - Throws:
java.lang.NullPointerException- if thexmlis null- Since:
- 9.5.5
-
getSettlementInfo
public static java.util.Optional<SettlementInfo> getSettlementInfo(java.lang.String xml)
Extracts settlement information from the given XML document.This method attempts to parse the provided XML and extract information related to the settlement method and clearing system codes. Specifically:
SttlmMtd- The settlement method.ClrSys > Cd- The clearing system code.ClrSys > Prtry- The clearing system proprietary.
If any of these elements are found, a
SettlementInfoobject is created and populated with the extracted values.- Parameters:
xml- the XML document as aStringto parse for settlement information.- Returns:
- an
Optionalcontaining theSettlementInfoif at least one of the required elements is found; otherwise, an emptyOptional. - Throws:
java.lang.NullPointerException- if thexmlis null.- Since:
- 9.5.5
-
findByTags
public static java.util.Optional<java.lang.String> findByTags(java.lang.String xml, java.lang.String... tags)Finds an XML element within a document by traversing a specified tag hierarchy with potential any element in between the specified tags.This method uses an
XMLStreamReaderto parse the provided XML document. It searches for an element that matches the specified sequence of tag names (hierarchy). For example, to find the<Cd>tag within<ClrSys>, you would call:findElement(xml, "ClrSys", "Cd");Notice that the<Cd>tag does not need to be a direct child of the<ClrSys>tag.It is important to remark the last tag in the sequence is the one that is expected to have text content, and not an intermediate tag.
- Parameters:
xml- the XML document as aStringto search.tags- the sequence of tag names that define the hierarchy of the target element.- Returns:
- an
Optionalcontaining the matching element value or empty if not found. - Throws:
java.lang.NullPointerException- if thexmlortagsare null.java.lang.IllegalArgumentException- if thexmlis a blank string.- Since:
- 9.5.6
-
findElementByTags
@Deprecated @ProwideDeprecated(phase2=SRU2025) public static java.util.Optional<javax.xml.stream.XMLStreamReader> findElementByTags(java.lang.String xml, java.lang.String... tags)Deprecated.usefindByTags(String, String...)instead
-
findByPath
public static java.util.Optional<java.lang.String> findByPath(java.lang.String xml, java.lang.String targetPath)Finds an XML element within a document by traversing a specified tag hierarchy.This method uses an
XMLStreamReaderto parse the provided XML document. It searches for an element that matches the specified path. For example, to find the<PgNb>you can call using absolute path or relative path: For relative path parameters you must use "//" as starting segment.findByPath(xml, /Document/BkToCstmrStmt/GrpHdr/MsgPgntn/PgNb); findByPath(xml, //BkToCstmrStmt/GrpHdr/MsgPgntn/PgNb);It is important to remark the last tag in the sequence is the one that is expected to have text content, and not an intermediate tag.
- Parameters:
xml- the XML document as aStringto search.targetPath- the path of the field to find into the xml, could be absolute or relative (indicated with starting double slash)- Returns:
- an
Optionalcontaining the found element value or empty if not found. - Throws:
java.lang.NullPointerException- if thexmlortagsare null.java.lang.IllegalArgumentException- if thexmlis a blank string.- Since:
- 9.5.6
-
findElementByPath
@Deprecated @ProwideDeprecated(phase2=SRU2025) public static java.util.Optional<javax.xml.stream.XMLStreamReader> findElementByPath(java.lang.String xml, java.lang.String targetPath)Deprecated.usefindByTags(String, String...)instead
-
elementExists
public static boolean elementExists(java.lang.String xml, java.lang.String localName)Checks if an element exists in the XML- Parameters:
xml- the XML contentlocalName- the element name- Returns:
- true if at least one element with the given name is found
- Since:
- 9.5.5
-
-