React Native
React Native
XML SearchForTag Method
See more XML Examples
Demonstrates the SearchForTag and SearchForTag2 methods.The input XML, available at http://www.chilkatsoft.com/data/fruitSearch.xml, is this:
<root>
<searchRoot>
<fruit color="red">apple</fruit>
<fruit color="green">pear</fruit>
<veg color="orange">carrot</veg>
<meat animal="cow">beef</meat>
<xyz>
<fruit color="blue">blueberry</fruit>
<veg color="green">broccoli</veg>
</xyz>
<fruit color="purple">grape</fruit>
<cheese color="yellow">cheddar</cheese>
</searchRoot>
<fruit color="red">strawberry</fruit>
<fruit color="orange">peach</fruit>
</root>
Chilkat React Native Downloads
import { Xml } from '@chilkat/react-native'
function chilkatExample() {
let success = false;
const xml = new Xml();
// The sample input XML is available at http://www.chilkatsoft.com/data/fruitSearch.xml
success = true;
try {
xml.loadXmlFile('qa_data/xml/fruitSearch.xml');
} catch {
success = false;
}
if (!success) {
console.log(xml.lastErrorText);
return;
}
// Search the sub-tree rooted at "searchRoot";
const xSearchRoot = xml.findChild('searchRoot');
// Search for all nodes having the tag "fruit";
let xBeginAfter = xSearchRoot.getSelf();
let xFound = xSearchRoot.searchForTag('fruit', xSearchRoot);
while (xSearchRoot.lastMethodSuccess) {
console.log(xFound.content);
xBeginAfter = xFound;
xFound = xSearchRoot.searchForTag('fruit', xBeginAfter);
}
console.log('--------------------------');
// ---------------------------------------------------------------------------------
// Now do the same, but instead use SearchForTag2
// which updates the internal reference of the caller instead
// of returning the found node.
xBeginAfter = xSearchRoot.getSelf();
const xSearch = xSearchRoot.getSelf();
success = true;
try {
xSearch.searchForTag2('fruit', xBeginAfter);
} catch {
success = false;
}
while (success) {
console.log(xSearch.content);
// Copy the internal references so that the next search
// begins after the found node.
xBeginAfter.copyRef(xSearch);
xSearch.copyRef(xSearchRoot);
success = true;
try {
xSearch.searchForTag2('fruit', xBeginAfter);
} catch {
success = false;
}
}
}