Abhishek Kumar Maurya
Abhishek Kumar Maurya

Reputation: 125

xmlConfig.configurationAt is not fetching value

I have a xml something like this.

<card>
    <name>VISA</name>
    <id>0</id>
    <length>
        <value>16</value>
    </length>
    <bin>
        <range>
            <start>4</start>
        </range>
    </bin>
</card>

Here is my code that I am using to load this and parse this xml.

private XMLConfiguration loadInputXml(final String responseXml)
        throws ConfigurationException {
    final XMLConfiguration xmlConfig = new XMLConfiguration();
    xmlConfig.setDelimiterParsingDisabled(true);
    xmlConfig.setValidating(false);
    xmlConfig.load(new ByteArrayInputStream(responseXml.getBytes()));
    return xmlConfig;
}

Now I am trying to fetch the values by this code

XMLConfiguration xmlConfig = loadInputXml(xmlString);
List<HierarchicalConfiguration> cardList = xmlConfig
        .configurationsAt("card");

I am not able to fetch the child nodes and I am not getting any error therefore I am not able to find the root cause of it. Need help. Thanks in advance!!!!

Upvotes: 0

Views: 205

Answers (1)

Nicolas Filotto
Nicolas Filotto

Reputation: 45005

xmlConfig.configurationAt is not fetching any value because card is the root element of your XML so calling configurationsAt("card") will make it search for an element card inside your root element and here you have no such element so you get nothing.

For me what you try to do, doesn't make much sense but if for an unknown reason you really need to access to the root element with configurationsAt use rather an empty String as parameter to make it fetch the root element as next:

List<HierarchicalConfiguration> cardList = xmlConfig.configurationsAt("");

What you should rather do is simply use your instance of the class XMLConfiguration to access to your properties with one of the getXXX methods according to the expected type.

For example let's say that I want to get the value of the name and the length, I would proceed as next:

XMLConfiguration xmlConfig = ...
// Get as String the text content of the element name
String name = xmlConfig.getString("name");
// Get as int the text content of the element value of the first element length
int length = xmlConfig.getInt("length(0).value");

Upvotes: 1

Related Questions