我用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>类似于示例,我的数组中有大量字符串,数组中有近250个这样的字符串元素。但是我只需要在我的android项目中加载一个项目,就可以做到这一点了。
我知道,我能做到
String[] k=getResources().getStringArray(R.array.Descriptions);
String Required=k[10];但是它消耗了大量的记忆,因为我正在装载大量的垃圾。因此,我只需要从该数组中加载单个项目(假设我只需要加载第10个元素)到我的项目中,以使其更高效。
我正在准备一个动态应用程序,所以我需要从这个数组加载不同的字符串,这取决于用户想要什么。
有什么办法吗,谢谢
发布于 2014-12-18 12:33:46
我认为您是在谈论resource xml文件,对吗?如果我说的不对,请忽略我的回答。
如果我是对的,现在提出的答案不会对你有帮助。原因是资源文件没有打包在apk文件中。它们是预先构建成二进制格式的,因此您不能查询它们。我的建议是重新考虑使用资源文件的方法,但使用本地数据库(SQLite)。这样,您将加载到内存中的字符串将仅为查询数据库的字符串。另一种方法(如果仍然希望使用xml格式)是将其存储为asset而不是resource文件。资产是在apk中交付的,因此使用SaxParser或XPathApis的方法将是有用的。
希望这对你有用,并帮助你解决问题:)
发布于 2014-12-18 11:12:24
您只需要访问数组的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());
}
}
}输出
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.发布于 2014-12-18 11:22:06
为了节省内存,您可以使用Sax解析器。
首先定义一个处理程序类,在解析时提取数据,类似于这样(未测试):
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();https://stackoverflow.com/questions/27542806
复制相似问题