【问题标题】:capitalize first letter of a xml element将 xml 元素的首字母大写
【发布时间】:2012-04-01 21:24:50
【问题描述】:

我是新手,请耐心等待。 我有以下代码检索节点及其罚款。我试图让“状态”节点的第一个字母大写但收效甚微,它强制关闭。

我所做的是将元素转换为字符串。我发现我可以对所有元素“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);
        }

【问题讨论】:

  • 如果您粘贴错误日志/堆栈跟踪会更好。
  • 04-01 22:32:34.928: W/dalvikvm(5306): threadid=1: 线程以未捕获的异常退出 (group=0x2aac8578) 04-01 22:32:34.928: E/AndroidRuntime (5306): 致命异常: 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
  • 线程 [ main] (Suspended (exception RuntimeException)) ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) 行:1659 ActivityThread.handleLaunchActivity(ActivityThread$ActivityClientRecord, Intent) 行:1675 ActivityThread。 access$1500(ActivityThread, ActivityThread$ActivityClientRecord, Intent) line: 121 ActivityThread$H.handleMessage(Message) line: 943 ActivityThread$H(Handler).dispatchMessage(Message) line: 99 Looper.loop() line: 130 ActivityThread.main (String[]) 行:3701
  • 如果删除大写部分,只在toast中显示字符串,应用程序是否运行?
  • 希望你解决了你的问题?

标签: android string nodes capitalization


【解决方案1】:

试试这个,

str = str.substring(0, 1).toLowerCase().concat(str.substring(1));

【讨论】:

    【解决方案2】:

    我使用以下代码解决了这个问题:

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

    【讨论】:

      猜你喜欢
      • 2019-07-29
      • 2019-04-20
      • 2012-09-06
      • 1970-01-01
      • 2021-07-29
      • 2011-05-14
      • 1970-01-01
      • 2011-06-16
      • 1970-01-01
      相关资源
      最近更新 更多