0

JavaでXmlファイルを1行ずつ読み取る必要があります。

ファイルには次の形式の行があります。

    <CallInt xsi:type="xsd:int">124</CallInt>

上記の行からタグ名 CallInt と値 124 のみを取得する必要があります。String Tokenizer、Split などを使用してみましたが、何も解決しませんでした。

誰でもこれで私を助けることができますか?

いくつかのコード

    BufferedReader buf = new BufferedReader(new FileReader(myxmlfile));

    while((line = buf.readLine())!=null)
    {
    String s = line;
    // Scanning for the tag and the integer value code???
    }
4

2 に答える 2

0

小さな xml パーサーを実際に使用する必要があります。

行ごとに読む必要があり、形式が行ベースであることが保証されている場合は、indexOf() で抽出するコンテンツの前後の区切り文字を検索してから、substring() を使用します...

int cut0 = line.indexOf('<');
if (cut0 != -1) {
  int cut1 = line.indexOf(' ', cut0);
  if (cut1 != -1) {
    String tagName = line.substring(cut0 + 1, cut1);

    int cut2 = line.indexOf('>', cut1);  // insert more ifs as needed...
    int cut3 = line.indexOf('<', cut2);

    String value = line.substring(cut2 + 1, cut2);
  }
}
于 2013-11-08T21:37:55.077 に答える