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 に答える