2012-04-01 102 views
0

我是新手,请耐心等待。 我有以下代码检索节点和罚款。我试图让“状态”节点获得第一个字母大写且没有什么成功的地方,它就会关闭。大写xml元素的第一个字母

我所做的是将元素转换为字符串。我发现我可以对所有元素'e'使用大写代码,但我宁愿将它用于状态。 为什么强迫关闭? 有人可以帮助我吗?

NodeList nodes = doc.getElementsByTagName("line"); 

    for (int i = 0; i < nodes.getLength(); i++) {       
     HashMap<String, String> map = new HashMap<String, String>();  

     Element e = (Element)nodes.item(i); 

     map.put("id", XMLFunctions.getValue(e, "id")); 
     map.put("name", XMLFunctions.getValue(e, "name")); 
     map.put("status", XMLFunctions.getValue(e, "status")); 
     map.put("message", XMLFunctions.getValue(e, "message")); 

     mylist.add(map); 

//element to string 
     Document document = e.getOwnerDocument(); 
     DOMImplementationLS domImplLS = (DOMImplementationLS) document 
      .getImplementation(); 
     LSSerializer serializer = domImplLS.createLSSerializer(); 
     String str = serializer.writeToString(e); 

//capitalization 
     if (str.length() <= 1) { 
      str = str.toLowerCase(); 
     } else { 
      str = str.substring(0, 1).toLowerCase() + str.substring(1); 
     } 
+0

。 – 2012-04-01 21:28:27

+0

04-01 22:32:34.928:W/dalvikvm(5306):threadid = 1:线程以未捕获的异常退出(group = 0x2aac8578) 04-01 22:32:34.928:E/AndroidRuntime(5306):FATAL EXCEPTION :main 04-01 22:32:34.928:E/AndroidRuntime(5306):java.lang.RuntimeException:无法启动活动ComponentInfo {augment.reality.app/augment.reality.app.Service}:java.lang。 ClassCastException异常:org.apache.harmony.xml.dom.DOMImplementationImpl – Pete 2012-04-01 21:34:05

+0

线程[<1>主](暂停(例外的RuntimeException))\t \t ActivityThread.performLaunchActivity(ActivityThread $ ActivityClientRecord,意图)线:1659 \t \t ActivityThread.handleLaunchActivity(ActivityThread $ ActivityClientRecord,Intent)行:1675 \t \t ActivityThread.access $ 1500(ActivityThread,ActivityThread $ ActivityClientRecord,意图)线:121 \t \t $ ActivityThread H.handleMessage(消息)线:943 \t \t $ ActivityThread H(处理程序).dispatchMessage(消息)线:99 \t \t Looper.loop()线:130 \t \t ActivityThread.main(字符串[])线:3701 – Pete 2012-04-01 21:42:20

回答

0

我解决了这个使用下面的代码:如果粘贴错误日志/堆栈跟踪效果会更好

public static String getValue(Element item, String str) {  


     NodeList n = item.getElementsByTagName(str); 

       char[] chars = XMLFunctions.getElementValue(n.item(0)).toLowerCase().toCharArray(); 
       boolean found = false; 
       for (int i = 0; i < chars.length; i++) { 
       if (!found && Character.isLetter(chars[i])) { 
        chars[i] = Character.toUpperCase(chars[i]); 
        found = true; 
       } else if (Character.isWhitespace(chars[i]) || chars[i]=='.' || chars[i]=='\'') { // You can add other chars here 
        found = false; 
       } 
       } 

       return String.valueOf(chars); 
0

试试这个,

str = str.substring(0, 1).toLowerCase().concat(str.substring(1)); 
相关问题