在我的Android应用程序中,我有
<string name="key1">Hello \n world </string>
及其打印
您好
世界
但是当我把它放在资产上并尝试从那里读取字符串时\n
不起作用。
这是我的资产api.xml
<string name="key1" lang_iso="en">Hello \n world</string>
我使用XML解析器并将其放在HashMap上以获取它。
private boolean parseData(String response){
HashMap<String,HashMap<String,String>> mainHashMap = new HashMap<String, HashMap<String, String>>();
XmlMGParser config = new XmlMGParser(response);
for(XmlMGParser element:config.children("string")){
String name = element.string("name");
String lang = element.string("lang_iso");
if (!mainHashMap.containsKey(lang)){
mainHashMap.put(lang,new HashMap<String, String>());
}
HashMap<String,String> hashMap = mainHashMap.get(lang);
hashMap.put(name, element.content());
}
return true;
}
它会出现什么问题?或者它是因为XMLMGParser引起的?
XMLMGParser.java
public class XmlMGParser {
private String name;
private String content;
private Map<String,String> nameAttributes = new HashMap<String,String>();
private Map<String,ArrayList<XmlMGParser>> nameChildren = new HashMap<String,ArrayList<XmlMGParser>>();
public XmlMGParser(InputStream inputStream, String rootName) {
this(rootElement(inputStream,rootName));
}
public XmlMGParser(String filename, String rootName) {
this(fileInputStream(filename),rootName);
}
public XmlMGParser(String xml) {
this(rootElement(xml));
}
private XmlMGParser(Element element) {
this.name = element.getNodeName();
this.content = element.getTextContent();
NamedNodeMap namedNodeMap = element.getAttributes();
int n = namedNodeMap.getLength();
for(int i=0;i<n;i++) {
Node node = namedNodeMap.item(i);
String name = node.getNodeName();
addAttribute(name,node.getNodeValue());
}
NodeList nodes = element.getChildNodes();
n = nodes.getLength();
for(int i=0;i<n;i++) {
Node node = nodes.item(i);
int type = node.getNodeType();
if(type==Node.ELEMENT_NODE) {
XmlMGParser child = new XmlMGParser((Element)node);
addChild(node.getNodeName(),child);
}
}
}
public void addAttribute(String name, String value) {
nameAttributes.put(name,value);
}
private void addChild(String name, XmlMGParser child) {
ArrayList<XmlMGParser> children = nameChildren.get(name);
if(children==null) {
children = new ArrayList<XmlMGParser>();
nameChildren.put(name,children);
}
children.add(child);
}
public String name() {
return name;
}
public void setContent(String content) {
this.content = content;
}
public String content() {
return content;
}
public void addChild(XmlMGParser xml) {
addChild(xml.name(),xml);
}
public void addChildren(XmlMGParser... xmls) {
for(XmlMGParser xml:xmls) addChild(xml.name(),xml);
}
public XmlMGParser child(String name) {
XmlMGParser child = optChild(name);
if(child==null) throw new RuntimeException("Could not find child node: "+name);
return child;
}
public XmlMGParser optChild(String name) {
ArrayList<XmlMGParser> children = children(name);
int n = children.size();
if(n>1) throw new RuntimeException("Could not find individual child node: "+name);
return n==0 ? null : children.get(0);
}
public boolean option(String name) {
return optChild(name)!=null;
}
public ArrayList<XmlMGParser> children(String name) {
ArrayList<XmlMGParser> children = nameChildren.get(name);
return children==null ? new ArrayList<XmlMGParser>() : children;
}
public String string(String name) {
String value = optString(name);
if(value==null) {
throw new RuntimeException(
"Could not find attribute: "+name+", in node: "+this.name);
}
return value;
}
public String optString(String name) {
return nameAttributes.get(name);
}
public int integer(String name) {
return Integer.parseInt(string(name));
}
public Integer optInteger(String name) {
String string = optString(name);
return string==null ? null : integer(name);
}
public double doubleValue(String name) {
return Double.parseDouble(optString(name));
}
public Double optDouble(String name) {
String string = optString(name);
return string==null ? null : doubleValue(name);
}
private static Element rootElement(InputStream inputStream, String rootName) {
try {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document document = builder.parse(inputStream);
Element rootElement = document.getDocumentElement();
if(!rootElement.getNodeName().equals(rootName))
throw new RuntimeException("Could not find root node: "+rootName);
return rootElement;
}
catch(IOException exception) {
throw new RuntimeException(exception);
}
catch(ParserConfigurationException exception) {
throw new RuntimeException(exception);
}
catch(SAXException exception) {
throw new RuntimeException(exception);
}
finally {
if(inputStream!=null) {
try {
inputStream.close();
}
catch(Exception exception) {
throw new RuntimeException(exception);
}
}
}
}
private static Element rootElement(String xml){
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Document dom=null;
try {
DocumentBuilder builder = factory.newDocumentBuilder();
StringReader reader = new StringReader(xml);
InputSource source = new InputSource(reader);
dom = builder.parse(source);
} catch (ParserConfigurationException e) {
} catch (SAXException e) {
} catch (IOException e) {
}
Element root = dom.getDocumentElement();
return root;
}
private static FileInputStream fileInputStream(String filename) {
try {
return new FileInputStream(filename);
}
catch(IOException exception) {
throw new RuntimeException(exception);
}
}
}
答案 0 :(得分:3)
字符串使用HTML格式,而不是/n
,而应使用<BR>
。
<string name="key1">Hello <BR> world </string>
答案 1 :(得分:0)
此外,如果您将字符串用作HTML,则可以使用&lt; br /&gt;换行
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">Hello<br />world!</string>
答案 2 :(得分:0)
尝试:
<string name="key1">"Hello \n world "</string>
您需要在字符串周围添加“”以应用\ n。