1

次のようなxmlファイルがあります

<Moves>
  <Move name="left">
     <Coord X="100" Y="100"/>
     <Coord X="50" Y="100"/>
  </Move>
  <Move name="right">
     <Coord X="10" Y="80"/>
     <Coord X="40" Y="90"/>
  </Move>
<Moves> 

SAX Parser を使用して Java で解析しています。次の2つの方法は基本的にそれを解析します

public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

                if (qName.equalsIgnoreCase("Coord")){
                    X = Integer.parseInt(attributes.getValue("X"));
                    Y = Integer.parseInt(attributes.getValue("Y"));
                } else if (qName.equalsIgnoreCase("Move")) {
                    move_points.clear();
                    move_name = attributes.getValue("name");
                }
            }

            /* If the read element is Move, add a MoveList with the name and if it is
             * a Coord, create a Point with it.
             */
            @Override
            public void endElement(String uri, String localName, String qName) throws SAXException {

                if (qName.equalsIgnoreCase("Coord")){
                    move_points.add(new Points(X, Y));
                } else if (qName.equalsIgnoreCase("Move")) {
                    moves_list.add(new MovesList(move_name, move_points));
                }
          }

読み取ったすべての座標を格納する ArrayList move_points と、移動名とその座標を格納する Arraylist move_list があります (これは arraylist - ここでは move_points です)

私が抱えている問題は、ドキュメントが解析されたときに、moves_list 内にあるすべての要素が正しい名前を持っているが、move_points のエントリまたは保存されている coord が XML ファイルの最後の移動のものであることです。

各要素の移動後に endElement メソッドで moves_list に入力されている内容を確認すると、moves_list に入力されている正しい座標が表示されますが、ドキュメント全体が解析され、ルート要素 Moves が解析された後に moves_list の内容が表示されます。最後の移動のすべての座標を含む moves_list を取得しています。

私を助けてください。

PS。move_list は public static 変数です

MoveList クラス

public class MovesList {

private ArrayList<Points> move_points;
private String move_name;

public MovesList (String move_name, ArrayList<Points> move_points) {
    this.move_name = move_name;
    this.move_points = move_points;
}

public String getName(){
    return move_name;
}

public ArrayList<Points> getPoints(){
    return move_points;
}

}

ポイントクラス

public class Points extends Point {

private int X;
private int Y;

public Points (int X, int Y) {
    this.X = X;
    this.Y = Y;
}

public Points (Points p) {
    X = p.getIntX();
    Y = p.getIntY();
}

public int getIntX () {
    return X;
}

public int getIntY () {
    return Y;
}

}
4

2 に答える 2

2

あなたの問題は、新しい move_points オブジェクトを作成しないことだと思います。したがって、この:

} else if (qName.equalsIgnoreCase("Move")) {
  move_points.clear();
  move_name = attributes.getValue("name");
}

これでなければなりません:

} else if (qName.equalsIgnoreCase("Move")) {
  move_points = new ArrayList<Points>(); // note difference
  move_name = attributes.getValue("name");
}

そうしないと、各 MovesList オブジェクトには、同じオブジェクトを参照する move_points 変数が含まれます。

于 2012-07-28T01:36:11.203 に答える
1

move_points という名前の変数があり、新しい MovesList を作成するときは、points という名前の変数を使用します。それはタイプミスですか?また、新しいMove要素を開始するときにmove_pointsを共有してクリアしているように見えるので、MovesListを作成するときにListをコピーしているといいのですが。

于 2012-07-28T01:04:54.197 に答える