Sample code for 30+ languages & platforms
React Native

Demonstrate XML SearchForTag2

Demonstrates how to use SearchForTag2.

Chilkat React Native Downloads

React Native
import { StringBuilder, Xml } from '@chilkat/react-native'

function chilkatExample() {
  const xml = new Xml();

  // Load the following XML:

  // 	<Test>
  // 	    <testchild1>
  // 	        test
  // 	    </testchild1>
  // 	    <testchild1>
  // 	        test1
  // 	    </testchild1>
  // 	    <testchild1>
  // 	        test2
  // 	    </testchild1>
  // 	    <testchild1>
  // 	        test3
  // 	    </testchild1>
  // 	    <testchild1>
  // 	        test4
  // 	    </testchild1>
  // 	    <testchild1>
  // 	        test5
  // 	    </testchild1>
  // 	    <testchild1>
  // 	        test6
  // 	    </testchild1>
  // 	</Test>

  xml.loadXml('<Test><testchild1>test</testchild1><testchild1>test1</testchild1><testchild1>test2</testchild1><testchild1>test3</testchild1><testchild1>test4</testchild1><testchild1>test5</testchild1><testchild1>test6</testchild1></Test>');

  let i = 0;
  let xmlSearch = xml.getRoot();
  let afterPtr = xml.getRoot();

  // Alway begin the search from the root.
  while (true) {
    try {
      xmlSearch.searchForTag2('testchild1', afterPtr);
    } catch {
      break;
    }
    // If successful, xmlSearch now points to the found element.
    i++;

    // Set afterPtr = xmlSearch (so we find the next match after the one we just found)
    // Rest xmlSearch to the root of the XML tree to be searched.
    console.log(`${i}: ${xmlSearch.content}`);
    const pTemp = afterPtr;
    afterPtr = xmlSearch;
    pTemp.getRoot2();
    xmlSearch = pTemp;
  }

  console.log('----');

  // A better way:
  const sbState = new StringBuilder();
  i = 0;
  while (xml.nextInTraversal2(sbState)) {
    if (xml.tagEquals('testchild1')) {
      console.log(`${i}: ${xml.content}`);
      i++;
    }
  }

  xml.getRoot2();
}