Ashok Varma
Ashok Varma

Reputation: 3539

How to get reference to a single element from a string-array defined in XML in Android

I had pretty massive string array defined in XML; For example

<string-array name="Descriptions">
    <item>A good raiding strategy is to fill your Army Camp with Giants and Archers; together they can easily wipe out a base. Use the Giants to distract all the Defensive Towers while the Archers take everything down.\nAt the beginning of a Raid, send in several Giants closest to the Mortar(s)/Wizard Tower(s). As soon as the Mortar hits the Giants, send in the necessary number of Wall Breakers to help the Giants break through the walls. Once the Mortar(s) and Wizard Tower(s) are destroyed, you can send in swarms of lower hit point Troops such as Goblins and Archers.\nGiants have a lot of health but are slow and have low attack power, so use them in groups.\nGiants are weak against many defenses grouped together.\nYou can use Giants as a cheap meat shield while deploying other, higher damaging troops to destroy buildings.\nAnother strategy is to send many Giants with one or more Healers. Be wary of Air Defenses and other anti-air towers, however, as Healers are very weak and have many vulnerabilities. \nIn general, Giants make better distracting troops than Barbarians due to their much higher health. Because of this they are less vulnerable to Mortar fire and other splash damage, and are much harder to kill.\nGiants ignore defensive troops (Clan Castle Troops, Barbarian King and Archer Queen) while defensive buildings can be targeted. Be sure to eliminate defensive troops to protect Giants.</item>
    <item>A good raiding strategy is to fill your Army Camp with Giants and Archers; together they can easily wipe out a base. Use the Giants to distract all the Defensive Towers while the Archers take everything down.\nAt the beginning of a Raid, send in several Giants closest to the Mortar(s)/Wizard Tower(s). As soon as the Mortar hits the Giants, send in the necessary number of Wall Breakers to help the Giants break through the walls. Once the Mortar(s) and Wizard Tower(s) are destroyed, you can send in swarms of lower hit point Troops such as Goblins and Archers.\nGiants have a lot of health but are slow and have low attack power, so use them in groups.\nGiants are weak against many defenses grouped together.\nYou can use Giants as a cheap meat shield while deploying other, higher damaging troops to destroy buildings.\nAnother strategy is to send many Giants with one or more Healers. Be wary of Air Defenses and other anti-air towers, however, as Healers are very weak and have many vulnerabilities. \nIn general, Giants make better distracting troops than Barbarians due to their much higher health. Because of this they are less vulnerable to Mortar fire and other splash damage, and are much harder to kill.\nGiants ignore defensive troops (Clan Castle Troops, Barbarian King and Archer Queen) while defensive buildings can be targeted. Be sure to eliminate defensive troops to protect Giants.</item>
    <item>A good raiding strategy is to fill your Army Camp with Giants and Archers; together they can easily wipe out a base. Use the Giants to distract all the Defensive Towers while the Archers take everything down.\nAt the beginning of a Raid, send in several Giants closest to the Mortar(s)/Wizard Tower(s). As soon as the Mortar hits the Giants, send in the necessary number of Wall Breakers to help the Giants break through the walls. Once the Mortar(s) and Wizard Tower(s) are destroyed, you can send in swarms of lower hit point Troops such as Goblins and Archers.\nGiants have a lot of health but are slow and have low attack power, so use them in groups.\nGiants are weak against many defenses grouped together.\nYou can use Giants as a cheap meat shield while deploying other, higher damaging troops to destroy buildings.\nAnother strategy is to send many Giants with one or more Healers. Be wary of Air Defenses and other anti-air towers, however, as Healers are very weak and have many vulnerabilities. \nIn general, Giants make better distracting troops than Barbarians due to their much higher health. Because of this they are less vulnerable to Mortar fire and other splash damage, and are much harder to kill.\nGiants ignore defensive troops (Clan Castle Troops, Barbarian King and Archer Queen) while defensive buildings can be targeted. Be sure to eliminate defensive troops to protect Giants.</item>

Similar to example i had massive strings in my array and nearly 250 such string elements in the ARRAY. But i need to load only one item in to my android project, is it possible to do that.

I know, I can do

String[] k=getResources().getStringArray(R.array.Descriptions);
String Required=k[10];

But it consumes lot of memory because i am loading ton of waste. so i need to load only single item (Suppose i need to load just the 10th element) from that array in to my project to make it more efficient.

I am preparing a dynamic app so i need to load different strings from that array depending on what user wants.

Is there any way to do that, Thankyou

Upvotes: 1

Views: 134

Answers (3)

Kiril Aleksandrov
Kiril Aleksandrov

Reputation: 2591

I think you are talking about resource xml file, right? If I am not right, please ignore my answer at all.

If I am right, the answers suggested for now won't help you. The reason is because resource files are not packaged within the apk file. They are prebuilt to a binary format so you cannot query them. My suggestion is to reconsider the approach with the resource files but use local database (SQLite). This way the string that you will load in the memory will be only those you query the database for. Another approach (if you still want to use the xml format) is to store it as an asset instead of a resource file. Assets are delivered as they are within the apk so the approaches with the SaxParser or the XPathApis will be useful.

Hope this was useful for you and helped you solve the problem :)

Upvotes: 2

syllabus
syllabus

Reputation: 581

To save memory you can use a Sax Parser.

First define a handler class to extract you data when parsing, smth like this (not tested):

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class ItemFinderHandler extends DefaultHandler {
 
    private int index;
    private int current;
    private String temp;
    private String foundItem = null;
 
    public SAXXMLHandler(int index) {
        this.index = index;
        this.current = 0;
    }
 
    public String getFoundItem() {
        return foundItem;
    }
 
    public void characters(char[] ch, int start, int length) throws SAXException {
        temp = new String(ch, start, length);
    }
 
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (qName.equalsIgnoreCase("item")) {
            if (index == current) {
                foundItem = temp;
            }
            current ++;
        }
    }
}

then do the parsing:

XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
ItemFinderHandler handler = new ItemFinderHandler(THE INDEX YOU WANT TO FETCH);
xmlReader.setContentHandler(handler);
xmlReader.parse(new InputSource(YOUR XML FILE));
String item = handler.getFoundItem();

Upvotes: 1

sol4me
sol4me

Reputation: 15698

You need to access only the 10 element of the array, You can use a query language to select the particular node in Xml.

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class XPathAndroidExtractor {

    public static void main(String[] args) throws XPathExpressionException, IOException, SAXException, ParserConfigurationException {
        String s = "<string-array name=\"Descriptions\"><item>A1 good raiding strategy is to fill your Army Camp with Giants and Archers; together they can easily wipe out a base. Use the Giants to distract all the Defensive Towers while the Archers take everything down.\\nAt the beginning of a Raid, send in several Giants closest to the Mortar(s)/Wizard Tower(s). As soon as the Mortar hits the Giants, send in the necessary number of Wall Breakers to help the Giants break through the walls. Once the Mortar(s) and Wizard Tower(s) are destroyed, you can send in swarms of lower hit point Troops such as Goblins and Archers.\\nGiants have a lot of health but are slow and have low attack power, so use them in groups.\\nGiants are weak against many defenses grouped together.\\nYou can use Giants as a cheap meat shield while deploying other, higher damaging troops to destroy buildings.\\nAnother strategy is to send many Giants with one or more Healers. Be wary of Air Defenses and other anti-air towers, however, as Healers are very weak and have many vulnerabilities. \\nIn general, Giants make better distracting troops than Barbarians due to their much higher health. Because of this they are less vulnerable to Mortar fire and other splash damage, and are much harder to kill.\\nGiants ignore defensive troops (Clan Castle Troops, Barbarian King and Archer Queen) while defensive buildings can be targeted. Be sure to eliminate defensive troops to protect Giants.</item>\n" +
                "    <item>A2 good raiding strategy is to fill your Army Camp with Giants and Archers; together they can easily wipe out a base. Use the Giants to distract all the Defensive Towers while the Archers take everything down.\\nAt the beginning of a Raid, send in several Giants closest to the Mortar(s)/Wizard Tower(s). As soon as the Mortar hits the Giants, send in the necessary number of Wall Breakers to help the Giants break through the walls. Once the Mortar(s) and Wizard Tower(s) are destroyed, you can send in swarms of lower hit point Troops such as Goblins and Archers.\\nGiants have a lot of health but are slow and have low attack power, so use them in groups.\\nGiants are weak against many defenses grouped together.\\nYou can use Giants as a cheap meat shield while deploying other, higher damaging troops to destroy buildings.\\nAnother strategy is to send many Giants with one or more Healers. Be wary of Air Defenses and other anti-air towers, however, as Healers are very weak and have many vulnerabilities. \\nIn general, Giants make better distracting troops than Barbarians due to their much higher health. Because of this they are less vulnerable to Mortar fire and other splash damage, and are much harder to kill.\\nGiants ignore defensive troops (Clan Castle Troops, Barbarian King and Archer Queen) while defensive buildings can be targeted. Be sure to eliminate defensive troops to protect Giants.</item>\n" +
                "    <item>A3 good raiding strategy is to fill your Army Camp with Giants and Archers; together they can easily wipe out a base. Use the Giants to distract all the Defensive Towers while the Archers take everything down.\\nAt the beginning of a Raid, send in several Giants closest to the Mortar(s)/Wizard Tower(s). As soon as the Mortar hits the Giants, send in the necessary number of Wall Breakers to help the Giants break through the walls. Once the Mortar(s) and Wizard Tower(s) are destroyed, you can send in swarms of lower hit point Troops such as Goblins and Archers.\\nGiants have a lot of health but are slow and have low attack power, so use them in groups.\\nGiants are weak against many defenses grouped together.\\nYou can use Giants as a cheap meat shield while deploying other, higher damaging troops to destroy buildings.\\nAnother strategy is to send many Giants with one or more Healers. Be wary of Air Defenses and other anti-air towers, however, as Healers are very weak and have many vulnerabilities. \\nIn general, Giants make better distracting troops than Barbarians due to their much higher health. Because of this they are less vulnerable to Mortar fire and other splash damage, and are much harder to kill.\\nGiants ignore defensive troops (Clan Castle Troops, Barbarian King and Archer Queen) while defensive buildings can be targeted. Be sure to eliminate defensive troops to protect Giants.</item>\n" +
                "    <item>A4 good raiding strategy is to fill your Army Camp with Giants and Archers; together they can easily wipe out a base. Use the Giants to distract all the Defensive Towers while the Archers take everything down.\\nAt the beginning of a Raid, send in several Giants closest to the Mortar(s)/Wizard Tower(s). As soon as the Mortar hits the Giants, send in the necessary number of Wall Breakers to help the Giants break through the walls. Once the Mortar(s) and Wizard Tower(s) are destroyed, you can send in swarms of lower hit point Troops such as Goblins and Archers.\\nGiants have a lot of health but are slow and have low attack power, so use them in groups.\\nGiants are weak against many defenses grouped together.\\nYou can use Giants as a cheap meat shield while deploying other, higher damaging troops to destroy buildings.\\nAnother strategy is to send many Giants with one or more Healers. Be wary of Air Defenses and other anti-air towers, however, as Healers are very weak and have many vulnerabilities. \\nIn general, Giants make better distracting troops than Barbarians due to their much higher health. Because of this they are less vulnerable to Mortar fire and other splash damage, and are much harder to kill.\\nGiants ignore defensive troops (Clan Castle Troops, Barbarian King and Archer Queen) while defensive buildings can be targeted. Be sure to eliminate defensive troops to protect Giants.</item>\n" +
                "    <item>A5 good raiding strategy is to fill your Army Camp with Giants and Archers; together they can easily wipe out a base. Use the Giants to distract all the Defensive Towers while the Archers take everything down.\\nAt the beginning of a Raid, send in several Giants closest to the Mortar(s)/Wizard Tower(s). As soon as the Mortar hits the Giants, send in the necessary number of Wall Breakers to help the Giants break through the walls. Once the Mortar(s) and Wizard Tower(s) are destroyed, you can send in swarms of lower hit point Troops such as Goblins and Archers.\\nGiants have a lot of health but are slow and have low attack power, so use them in groups.\\nGiants are weak against many defenses grouped together.\\nYou can use Giants as a cheap meat shield while deploying other, higher damaging troops to destroy buildings.\\nAnother strategy is to send many Giants with one or more Healers. Be wary of Air Defenses and other anti-air towers, however, as Healers are very weak and have many vulnerabilities. \\nIn general, Giants make better distracting troops than Barbarians due to their much higher health. Because of this they are less vulnerable to Mortar fire and other splash damage, and are much harder to kill.\\nGiants ignore defensive troops (Clan Castle Troops, Barbarian King and Archer Queen) while defensive buildings can be targeted. Be sure to eliminate defensive troops to protect Giants.</item>\n" +
                "    <item>A6 good raiding strategy is to fill your Army Camp with Giants and Archers; together they can easily wipe out a base. Use the Giants to distract all the Defensive Towers while the Archers take everything down.\\nAt the beginning of a Raid, send in several Giants closest to the Mortar(s)/Wizard Tower(s). As soon as the Mortar hits the Giants, send in the necessary number of Wall Breakers to help the Giants break through the walls. Once the Mortar(s) and Wizard Tower(s) are destroyed, you can send in swarms of lower hit point Troops such as Goblins and Archers.\\nGiants have a lot of health but are slow and have low attack power, so use them in groups.\\nGiants are weak against many defenses grouped together.\\nYou can use Giants as a cheap meat shield while deploying other, higher damaging troops to destroy buildings.\\nAnother strategy is to send many Giants with one or more Healers. Be wary of Air Defenses and other anti-air towers, however, as Healers are very weak and have many vulnerabilities. \\nIn general, Giants make better distracting troops than Barbarians due to their much higher health. Because of this they are less vulnerable to Mortar fire and other splash damage, and are much harder to kill.\\nGiants ignore defensive troops (Clan Castle Troops, Barbarian King and Archer Queen) while defensive buildings can be targeted. Be sure to eliminate defensive troops to protect Giants.</item>\n" +
                "    <item>A7 good raiding strategy is to fill your Army Camp with Giants and Archers; together they can easily wipe out a base. Use the Giants to distract all the Defensive Towers while the Archers take everything down.\\nAt the beginning of a Raid, send in several Giants closest to the Mortar(s)/Wizard Tower(s). As soon as the Mortar hits the Giants, send in the necessary number of Wall Breakers to help the Giants break through the walls. Once the Mortar(s) and Wizard Tower(s) are destroyed, you can send in swarms of lower hit point Troops such as Goblins and Archers.\\nGiants have a lot of health but are slow and have low attack power, so use them in groups.\\nGiants are weak against many defenses grouped together.\\nYou can use Giants as a cheap meat shield while deploying other, higher damaging troops to destroy buildings.\\nAnother strategy is to send many Giants with one or more Healers. Be wary of Air Defenses and other anti-air towers, however, as Healers are very weak and have many vulnerabilities. \\nIn general, Giants make better distracting troops than Barbarians due to their much higher health. Because of this they are less vulnerable to Mortar fire and other splash damage, and are much harder to kill.\\nGiants ignore defensive troops (Clan Castle Troops, Barbarian King and Archer Queen) while defensive buildings can be targeted. Be sure to eliminate defensive troops to protect Giants.</item>\n" +
                "    <item>A8 good raiding strategy is to fill your Army Camp with Giants and Archers; together they can easily wipe out a base. Use the Giants to distract all the Defensive Towers while the Archers take everything down.\\nAt the beginning of a Raid, send in several Giants closest to the Mortar(s)/Wizard Tower(s). As soon as the Mortar hits the Giants, send in the necessary number of Wall Breakers to help the Giants break through the walls. Once the Mortar(s) and Wizard Tower(s) are destroyed, you can send in swarms of lower hit point Troops such as Goblins and Archers.\\nGiants have a lot of health but are slow and have low attack power, so use them in groups.\\nGiants are weak against many defenses grouped together.\\nYou can use Giants as a cheap meat shield while deploying other, higher damaging troops to destroy buildings.\\nAnother strategy is to send many Giants with one or more Healers. Be wary of Air Defenses and other anti-air towers, however, as Healers are very weak and have many vulnerabilities. \\nIn general, Giants make better distracting troops than Barbarians due to their much higher health. Because of this they are less vulnerable to Mortar fire and other splash damage, and are much harder to kill.\\nGiants ignore defensive troops (Clan Castle Troops, Barbarian King and Archer Queen) while defensive buildings can be targeted. Be sure to eliminate defensive troops to protect Giants.</item>\n" +
                "    <item>A9 good raiding strategy is to fill your Army Camp with Giants and Archers; together they can easily wipe out a base. Use the Giants to distract all the Defensive Towers while the Archers take everything down.\\nAt the beginning of a Raid, send in several Giants closest to the Mortar(s)/Wizard Tower(s). As soon as the Mortar hits the Giants, send in the necessary number of Wall Breakers to help the Giants break through the walls. Once the Mortar(s) and Wizard Tower(s) are destroyed, you can send in swarms of lower hit point Troops such as Goblins and Archers.\\nGiants have a lot of health but are slow and have low attack power, so use them in groups.\\nGiants are weak against many defenses grouped together.\\nYou can use Giants as a cheap meat shield while deploying other, higher damaging troops to destroy buildings.\\nAnother strategy is to send many Giants with one or more Healers. Be wary of Air Defenses and other anti-air towers, however, as Healers are very weak and have many vulnerabilities. \\nIn general, Giants make better distracting troops than Barbarians due to their much higher health. Because of this they are less vulnerable to Mortar fire and other splash damage, and are much harder to kill.\\nGiants ignore defensive troops (Clan Castle Troops, Barbarian King and Archer Queen) while defensive buildings can be targeted. Be sure to eliminate defensive troops to protect Giants.</item>\n" +
                "    <item>A10 good raiding strategy is to fill your Army Camp with Giants and Archers; together they can easily wipe out a base. Use the Giants to distract all the Defensive Towers while the Archers take everything down.\\nAt the beginning of a Raid, send in several Giants closest to the Mortar(s)/Wizard Tower(s). As soon as the Mortar hits the Giants, send in the necessary number of Wall Breakers to help the Giants break through the walls. Once the Mortar(s) and Wizard Tower(s) are destroyed, you can send in swarms of lower hit point Troops such as Goblins and Archers.\\nGiants have a lot of health but are slow and have low attack power, so use them in groups.\\nGiants are weak against many defenses grouped together.\\nYou can use Giants as a cheap meat shield while deploying other, higher damaging troops to destroy buildings.\\nAnother strategy is to send many Giants with one or more Healers. Be wary of Air Defenses and other anti-air towers, however, as Healers are very weak and have many vulnerabilities. \\nIn general, Giants make better distracting troops than Barbarians due to their much higher health. Because of this they are less vulnerable to Mortar fire and other splash damage, and are much harder to kill.\\nGiants ignore defensive troops (Clan Castle Troops, Barbarian King and Archer Queen) while defensive buildings can be targeted. Be sure to eliminate defensive troops to protect Giants.</item>" +
                "</string-array>";
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8)));
        XPath xpath = XPathFactory.newInstance().newXPath();
        XPathExpression descriptionExpr = xpath.compile("//item[10]/text()");
        Object result = descriptionExpr.evaluate(doc, XPathConstants.NODESET);
        NodeList nodes = (NodeList) result;
        for (int i = 0; i < nodes.getLength(); i++) {
            System.out.println(nodes.item(i).getNodeValue());
        }
    }
}

Output

A10 good raiding strategy is to fill your Army Camp with Giants and Archers; together they can easily wipe out a base. Use the Giants to distract all the Defensive Towers while the Archers take everything down.\nAt the beginning of a Raid, send in several Giants closest to the Mortar(s)/Wizard Tower(s). As soon as the Mortar hits the Giants, send in the necessary number of Wall Breakers to help the Giants break through the walls. Once the Mortar(s) and Wizard Tower(s) are destroyed, you can send in swarms of lower hit point Troops such as Goblins and Archers.\nGiants have a lot of health but are slow and have low attack power, so use them in groups.\nGiants are weak against many defenses grouped together.\nYou can use Giants as a cheap meat shield while deploying other, higher damaging troops to destroy buildings.\nAnother strategy is to send many Giants with one or more Healers. Be wary of Air Defenses and other anti-air towers, however, as Healers are very weak and have many vulnerabilities. \nIn general, Giants make better distracting troops than Barbarians due to their much higher health. Because of this they are less vulnerable to Mortar fire and other splash damage, and are much harder to kill.\nGiants ignore defensive troops (Clan Castle Troops, Barbarian King and Archer Queen) while defensive buildings can be targeted. Be sure to eliminate defensive troops to protect Giants.

Upvotes: 1

Related Questions