Met Text Substringdata
## XML DOM substringData() Method
The `substringData()` method is a built-in function of the XML DOM `Text` and `CharacterData` interfaces. It is used to extract a specific range of characters (a substring) from a text node.
---
## Definition and Usage
The `substringData()` method retrieves a specified portion of data from a text node without modifying the original node's content. This is highly useful when you need to parse, display, or manipulate specific segments of text retrieved from an XML document.
---
## Syntax
```javascript
textNode.substringData(start, length);
```
### Parameter Values
| Parameter | Type | Required / Optional | Description |
| :--- | :--- | :--- | :--- |
| `start` | Integer | **Required** | Specifies the zero-based index position where the character extraction begins. The first character is at index `0`. |
| `length` | Integer | **Required** | Specifies the number of characters to extract from the starting position. |
### Return Value
* **Type:** `String`
* **Description:** Returns the extracted substring. If the sum of `start` and `length` exceeds the total length of the text node, the method returns all characters from the `start` position to the end of the string.
---
## Code Example
The following example demonstrates how to load an XML document (`books.xml`) and extract a substring from the text node of the first `` element.
### Sample XML File (`books.xml`)
Assume our XML file contains the following structure:
```xml
Everyday Italian
Giada De Laurentiis
2005
30.00
```
### JavaScript Implementation
```javascript
// Load the XML document
const xmlDoc = loadXMLDoc("books.xml");
// Access the text node of the first element
const titleTextNode = xmlDoc.getElementsByTagName("title").childNodes;
// Extract 7 characters starting from index position 9
const substringResult = titleTextNode.substringData(9, 7);
// Output the original node value and the extracted substring
console.log("Original Text: " + titleTextNode.nodeValue);
// Output: Everyday Italian
console.log("Extracted Substring: " + substringResult);
// Output: Italian
```
### Output
```text
Original Text: Everyday Italian
Extracted Substring: Italian
```
---
## Considerations and Exceptions
When working with the `substringData()` method, keep the following rules in mind:
1. **Zero-Based Indexing:** The `start` parameter is zero-based. For example, to start extracting from the very first character, pass `0` as the `start` argument.
2. **Index Size Errors:**
* If the `start` parameter is negative or greater than the total number of characters in the text node, a `DOMException` with the code `INDEX_SIZE_ERR` will be thrown.
* If the `length` parameter is negative, a `DOMException` is also thrown.
3. **Handling Excess Length:** If the specified `length` is greater than the remaining characters available from the `start` position, the method does not throw an error; it simply returns all characters up to the end of the text node.
YouTip