0

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

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

2 回答 2

0

试试这个,

str = str.substring(0, 1).toLowerCase().concat(str.substring(1));
于 2012-04-01T21:27:36.010 回答
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);
于 2012-04-09T14:26:30.010 回答