Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Android中从XML中定义的字符串数组中获取对单个元素的引用_Java_Android_Arrays_Android Resources - Fatal编程技术网

Java 如何在Android中从XML中定义的字符串数组中获取对单个元素的引用

Java 如何在Android中从XML中定义的字符串数组中获取对单个元素的引用,java,android,arrays,android-resources,Java,Android,Arrays,Android Resources,我用XML定义了大量的字符串数组;比如说 <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 whi

我用XML定义了大量的字符串数组;比如说

<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>
但它消耗了大量内存,因为我加载了大量的垃圾。因此,我只需要加载单个项,假设我只需要将该数组中的第10个元素加载到我的项目中,以提高效率

我正在准备一个动态应用程序,所以我需要根据用户需要从该数组加载不同的字符串


有什么方法可以做到这一点吗,谢谢

您只需要访问数组的10元素,就可以使用来选择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());
        }
    }
}

您只需要访问数组的10元素,就可以使用来选择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());
        }
    }
}

为了节省内存,可以使用Sax解析器

首先定义一个处理程序类,以便在解析时提取数据,smth如下未测试:

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 ++;
        }
    }
}
然后进行分析:

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();

为了节省内存,可以使用Sax解析器

首先定义一个处理程序类,以便在解析时提取数据,smth如下未测试:

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 ++;
        }
    }
}
然后进行分析:

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();

我想您是在谈论资源xml文件,对吗?如果我不对,请不要理会我的回答

如果我是对的,现在建议的答案对你没有帮助。原因是资源文件未打包在apk文件中。它们预先构建为二进制格式,因此您无法查询它们。我的建议是重新考虑使用资源文件的方法,但使用本地数据库SQLite。这样,您将加载到内存中的字符串将仅是您在数据库中查询的字符串。 如果仍然希望使用xml格式,另一种方法是将其存储为资产,而不是资源文件。资产是在apk中交付的,因此使用SaxParser或XPathAPI的方法将非常有用


希望这对您有用并帮助您解决问题:

我想您是在谈论资源xml文件,对吗?如果我不对,请不要理会我的回答

如果我是对的,现在建议的答案对你没有帮助。原因是资源文件未打包在apk文件中。它们预先构建为二进制格式,因此您无法查询它们。我的建议是重新考虑使用资源文件的方法,但使用本地数据库SQLite。这样,您将加载到内存中的字符串将仅是您在数据库中查询的字符串。 如果仍然希望使用xml格式,另一种方法是将其存储为资产,而不是资源文件。资产是在apk中交付的,因此使用SaxParser或XPathAPI的方法将非常有用


希望这对您有用并帮助您解决问题:

您可以使用XPathAPI检查:您可以使用XPathAPI检查:我认为我们不能在android资源中使用查询语言。@AshokVarma您为什么这么认为,您能提供一些链接/事实吗?因为在apk构建过程中,所有android资源文件都预构建为二进制格式。将apk安装到手机时,没有此类文件。交付的唯一文件是资产文件。我认为我们不能在android资源中使用查询语言。@AshokVarma为什么这样认为,您能提供一些链接/事实吗?因为在apk构建过程中,所有android资源文件都预构建为二进制格式。将apk安装到手机时,没有此类文件。目前交付的唯一文件是资产文件。我认为我们不能在android资源中使用查询语言。我认为我们不能在android资源中使用查询语言。Aleksanndrov:除了使用数据库,你还知道其他方法吗。因为我必须再次学习数据库。java中的任何方法??您仍然可以使用文件。只是不要使用资源文件。运行时可以访问资产文件夹中的任何xml文件。您可以创建一个只包含数据而不包含其他资源的xml文件,并将其放在assets文件夹中。然后在代码中可以使用XmlPullParser来检索所需的内容。你可以看到一个例子:非常感谢,这个例子对我帮助很大,我没有足够的声誉去投票给答案,很抱歉。这对我很有帮助:除了使用数据库,你还知道其他方法吗。因为我必须再次学习数据库。java中的任何方法??您仍然可以使用文件。只是不要使用资源文件。运行时可以访问资产文件夹中的任何xml文件。您可以创建一个只包含数据而不包含其他资源的xml文件,并将其放在assets文件夹中。然后在代码中可以使用XmlPullParser来检索所需的内容。你可以看到一个例子:非常感谢,这个例子对我帮助很大,我没有足够的声誉去投票给答案,很抱歉。这对我帮助很大