XML.SubTree
-----------

Extracts a given subtree.

| Component | Version | macOS | Windows | Linux | Server | iOS SDK |
|---|---|---|---|---|---|---|
| [XML](component_XML.md) | [7.0](newinversion70.md) | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |

MBS( "XML.SubTree"; XML; Name { ; Flags; Index } ) 

**MBS**( **"XML.SubTree";** /\* Extracts a given subtree. \*/  
**$XML**; /\* The XML to process.  
Can be XML as text or the reference returned by [XML.Parse](XMLParse.md) function, so you can make several times changes to the XML without parsing it each time.e.g. "&lt;test&gt;Hello&lt;/test&gt;" \*/   
**$Name**; /\* The name of the node to query.  
This can be a list to go over several steps to the target tree part.e.g. "something" \*/   
**$Flags**; /\* Optional; Various Flags.  
Add 1 to ignore errors in xml and continue parsing. This may lead to not everything in the xml being read.  
Add 4 if you do not like the XML to be formatted.  
Add 8 to remove all namespaces before query to have queries easier. (new in 6.3)e.g. 0 \*/   
**$Index**) /\* Optional; The index of the item to get.  
First has index 0 as this counts down how many to skip.e.g. 0 \*/ 

### Parameters

| Parameter | Description | Example | Flags |
|---|---|---|---|
| XML | The XML to process.   Can be XML as text or the reference returned by [XML.Parse](XMLParse.md) function, so you can make several times changes to the XML without parsing it each time. | "&lt;test&gt;Hello&lt;/test&gt;" |  |
| Name | The name of the node to query.   This can be a list to go over several steps to the target tree part. | "something" |  |
| Flags | Various Flags.   Add 1 to ignore errors in xml and continue parsing. This may lead to not everything in the xml being read.   Add 4 if you do not like the XML to be formatted.   Add 8 to remove all namespaces before query to have queries easier. (new in 6.3) | 0 | Optional |
| Index | The index of the item to get.   First has index 0 as this counts down how many to skip. | 0 | Optional |

### Result

Returns xml or error message.

### Description

Extracts a given subtree.  
You can pass an index to get nth item.  
Returns empty result if index is too high, so you can loop until you get empty result.  
### Examples

Query person entry:

 MBS ( "XML.SubTree"; "&lt;People&gt;&lt;Person&gt;&lt;FirstName&gt;Markus&lt;/FirstName&gt;&lt;LastName&gt;Müller&lt;/LastName&gt;&lt;City&gt;New York&lt;/City&gt;&lt;/Person&gt;&lt;/People&gt;"; "Person")  
<small>  
Example result:   
&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;Person&gt;  
 &lt;FirstName&gt;Markus&lt;/FirstName&gt;  
 &lt;LastName&gt;Müller&lt;/LastName&gt;  
 &lt;City&gt;New York&lt;/City&gt;  
&lt;/Person&gt;</small>Queries values:

 Set Variable \[$xml ; Value:"&lt;a&gt;&lt;b&gt;123&lt;/b&gt;&lt;b&gt;456&lt;/b&gt;&lt;b&gt;789&lt;/b&gt;&lt;/a&gt;"\]  
Show Custom Dialog \["First item:"; MBS ( "[XML.ExtractText](XMLExtractText.md)"; MBS ( "XML.SubTree"; $xml ; "b"; 0; 0))\]  
Show Custom Dialog \["Second item:"; MBS ( "[XML.ExtractText](XMLExtractText.md)"; MBS ( "XML.SubTree"; $xml ; "b"; 0; 1))\]  
Show Custom Dialog \["Third item:"; MBS ( "[XML.ExtractText](XMLExtractText.md)"; MBS ( "XML.SubTree"; $xml ; "b"; 0; 2))\]  
Query data in SOAP Webservice response:

 MBS ( "XML.SubTree"; $XML ; "Envelope¶Body¶AdressCheckerResponse¶rows")  
Custom function to get xml value as text:

 Let (\[  
  
// GetXMLValue(xml, name)  
// returns text of a xml node  
  
part = MBS ( "XML.SubTree"; xml; name);  
result = If( MBS ("[IsError](IsError.md)"); ""; If(Length(part) = 0; ""; MBS ( "[XML.ExtractText](XMLExtractText.md)"; part)))  
\]; Trim( result))  
Create records and fill fields with xml values:

 Go to Layout \[“XML Parser”\]  
\# find the node with all the records we need:   
Set Variable \[$oxml ; Value:MBS ( "XML.SubTree"; XML Parser::Input XML; "Body¶GetCategoryProductsResponse" )\]  
\# query number of products   
Set Variable \[$count ; Value:MBS ( "[XML.NodeCount](XMLNodeCount.md)"; $oxml ; "PRODUCT")\]  
\# loop over products   
Set Variable \[$index ; Value:0\]  
Loop  
 # get a product  
 Set Variable \[$xml ; Value:MBS ( "XML.SubTree"; $oxml ; "PRODUCT"; $index )\]  
 # get the names of the XML nodes  
 Set Variable \[$names ; Value:MBS ( "[XML.NodeNames](XMLNodeNames.md)"; $xml ; 0; 16)\]  
 # loop over the names  
 Set Variable \[$NameCount ; Value:ValueCount ( $names )\]  
 Set Variable \[$NameIndex ; Value:1\]  
 New Record/Request  
 Loop  
 # Query a value and put it in a field  
 Set Variable \[$name ; Value:GetValue($names ; $NameIndex )\]  
 Set Field By Name \["XML Parser::" &amp; $name ; GetXMLValue( $xml ; $name )\]  
 Set Variable \[$NameIndex ; Value:$NameIndex + 1\]  
 Exit Loop If \[$NameIndex ≥ $NameCount \]  
 End Loop  
 Commit Records/Requests \[No dialog\]  
 Set Variable \[$index ; Value:$index + 1\]  
 Exit Loop If \[$index &gt; $count \]  
End Loop  
### See also

- [IsError](IsError.md)
- [XML.ExtractText](XMLExtractText.md)
- [XML.NodeCount](XMLNodeCount.md)
- [XML.NodeNames](XMLNodeNames.md)
- [XML.Parse](XMLParse.md)

### Release notes

- **Version 13.3**
    - Fixed a possible bug in [XML.SubTree](https://www.mbsplugins.eu/XMLSubTree.shtml) function when XML is empty.
- **Version 7.0**
    - Added XML helper functions: [XML.ExtractText](http://www.mbsplugins.eu/XMLExtractText.shtml), [XML.NodeNames](http://www.mbsplugins.eu/XMLNodeNames.shtml), [XML.SetVariables](http://www.mbsplugins.eu/XMLSetVariables.shtml), [XML.ListAttributes](http://www.mbsplugins.eu/XMLListAttributes.shtml), XML.HasAttributes, XML.GetAttributes and [XML.SubTree](http://www.mbsplugins.eu/XMLSubTree.shtml).

### Example Databases

- [CURL/WebServices/magento older](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/CURL/WebServices/magento%20older.shtml#3ScriptAnchor_)
- [CURL/WebServices/Sales Force Test](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/CURL/WebServices/Sales%20Force%20Test.shtml#1ScriptAnchor_)
- [Utility functions/Split DDR](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/Utility%20functions/Split%20DDR.shtml#1ScriptAnchor_)
- [XML/XML Test](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/XML/XML%20Test.shtml#2ScriptAnchor_)

### Blog Entries

- [MBS FileMaker Plugin, version 13.3pr1](https://www.mbsplugins.de/archive/2023-06-01/MBS_FileMaker_Plugin_version_1/monkeybreadsoftware_blog_filemaker)
- [Split XML for DDR into files for GIT](https://www.mbsplugins.de/archive/2020-07-27/Split_XML_for_DDR_into_files_f/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 7.0pr1](https://www.mbsplugins.de/archive/2017-01-02/MBS_FileMaker_Plugin_version_7/monkeybreadsoftware_blog_filemaker)
- [New XML Functions in MBS FileMaker Plugin](https://www.mbsplugins.de/archive/2016-12-27/New_XML_Functions_in_MBS_FileM/monkeybreadsoftware_blog_filemaker)

### FileMaker Magazin

- [Ausgabe 3/2025, Seite 19](https://filemaker-magazin.de/neuigkeit/4290-Appetithappen-FMM_202503)
- [Ausgabe 3/2017, Seite 27](https://filemaker-magazin.de/neuigkeit/3938-Appetithappen-FMM_201703)

This function checks for a license.

Created 25th December 2016 , last changed 5th September 2025

  
[XML.SetVariables](XMLSetVariables.md) - [XML.Tidy](XMLTidy.md)

[HTML Version](XMLSubTree.shtml)