ftos.xml.load
Loads an XML string into an XDocument wrapper object.
Syntax
ftos.xml.load(xml: string): XDocumentWrapper;
| Parameter | Type | Description |
|---|---|---|
xml
|
string | XML-formatted string. |
Return Value
Returns an XDocumentWrapper object.
Interfaces
XDocumentWrapper
XML Document object based on the XDocument class, which serves as a wrapper around the standard .NET XDocument class. The wrapper provides a simplified interface for working with XML data, enabling access to elements, attributes, and queries without requiring direct interaction with the underlying XDocument object.
interface XDocumentWrapper {
root: XElementWrapper;
asString(): string;
query(xpath: string, namespaces: any): XElementWrapper[];
query(xpath: string): XElementWrapper[];
}
| Property | Type | Description |
|---|---|---|
root
|
XElementWrapper | Root node of the XDocumentWrapper. |
Returns the XML document as string.
Runs an XPath query on the XML document and returns the resulting XML elements in an array of XElementWrapper objects.
| Parameter | Type | Description |
|---|---|---|
xpath
|
string | Path expression of the XPath query. |
Runs an XPath query with namespaces on the XML document and returns the resulting XML elements in an array of XElementWrapper objects.
| Parameter | Type | Description |
|---|---|---|
xpath
|
string | Path expression of the XPath query. |
namespaces
|
any | JSON object that maps namespace URIs with XML element names. |
XElementWrapper
XML element within an XDocumentWrapper document, based on the standard .NET XElement class. Provides a structured way to access and manipulate individual XML elements, their attributes, and child elements.
interface XElementWrapper {
attributeCount: number;
elementCount: number;
hasAttributes: boolean;
hasElements: boolean;
readonly [index: string]: any;
name: string;
namespace: string;
value: string;
elements(): XElementWrapper[];
elements(name: string): XElementWrapper[];
getAttributeValue(attributeName: string): string;
query(xpath: string): XElementWrapper[];
query(xpath: string, namespaces: any): XElementWrapper[];
}
| Property | Type | Description |
|---|---|---|
attributeCount
|
number | Number of attributes of the XML element. |
elementCount
|
number | Number of child elements. |
hasAttributes
|
boolean |
|
hasElements
|
boolean |
|
index : string
|
any | Returns an attribute's value based on its key. To specify a namespace for the attribute, use the following syntax:'{namespace URI}key' |
name
|
string | Name of the XML element. |
namespace
|
string | Namespace associated with the XML element. |
value
|
string | Value of the XML element. |
Returns all child elements as an array of XElementWrapper objects.
Returns all child elements with the specified name as an array of XElementWrapper objects.
| Parameter | Type | Description |
|---|---|---|
name
|
string | Name of the child elements. To specify a namespace for the name, use the following syntax:'{namespace URI}key' |
Executes an XPath query on the XML element and returns the resulting XML elements in an array of XElementWrapper objects.
| Parameter | Type | Description |
|---|---|---|
xpath
|
string | Path expression of the XPath query. |
Executes an XPath query with namespaces on the XML element and returns the resulting XML elements in an array of XElementWrapper objects.
| Parameter | Type | Description |
|---|---|---|
xpath
|
string | Path expression of the XPath query. |
namespaces
|
any | JSON object that maps namespace URIs with XML element names. |
Example
This example:
- Loads an XML string representing a bank account into an XDocumentWrapper object.
- Accesses the root element and retrieves the customer’s ID, name, account balance, and currency from nested XML elements and attributes.
- Performs an XPath query to obtain the account status from the XML structure.
// Example XML string representing a simple bank account
const xmlData = `
<BankAccount>
<Customer id="C1001">
<Name>Jane Smith</Name>
<Balance currency="USD">24500.75</Balance>
<Status>Active</Status>
</Customer>
</BankAccount>
`;
// Load the XML string
const doc = ftos.xml.load(xmlData);
// Access the root element and customer node
const root = doc.root;
const customer = root.elements("Customer")[0];
// Read element and attribute values
const customerId = customer.getAttributeValue("id");
const name = customer.elements("Name")[0].value;
const balance = customer.elements("Balance")[0].value;
const currency = customer.elements("Balance")[0].getAttributeValue("currency");
// Read an element value using an XPath query
const statusNode = doc.query("//Status")[0];
console.log("Account Status:", statusNode.value);