React Native
React Native
Methods for Getting Attributes
See more XML Examples
Demonstrates some methods for getting attribute name/values.The input XML, available at http://www.chilkatsoft.com/data/car.xml, is this:
<root>
<car color="black" make="mercedes" model="C350" hp="302" engine="v6" type="sedan">Mercedes Benz C350</car>
</root>
Chilkat React Native Downloads
import { Xml } from '@chilkat/react-native'
function chilkatExample() {
const xml = new Xml();
let i = 0;
// The sample input XML is available at http://www.chilkatsoft.com/data/car.xml
try {
xml.loadXmlFile('car.xml');
} catch {
console.log(xml.lastErrorText);
return;
}
// Navigate to the "car" node, which is the 1st child:
const carNode = xml.firstChild();
// Get the value of the "model" attribute:
console.log(`model = ${carNode.getAttrValue('model')}`);
// Get the value of the "hp" attribute as an integer:
const horsepower = carNode.getAttrValueInt('hp');
console.log(`horsepower = ${horsepower}`);
// Iterate over the attributes and show the name/value of each:
const numAttr = carNode.numAttributes;
i = 0;
while (i < numAttr) {
console.log(`${carNode.getAttributeName(i)}: ${carNode.getAttributeValue(i)}`);
i++;
}
}