Android:SAX用相同的标签解析XML

时间:2014-01-11 13:07:41

标签: android xml tags sax

我想用SAX解析这种类型的文件:

<MotionGraph>
  <Nodes>
    <NodePoint>308 204 0</NodePoint>
    <NodePoint>67 202 0</NodePoint>
    <NodePoint>509 206 0</NodePoint>
  </Nodes>
  <Arcs>
    <Arc>14 2 35</Arc>
    <Arc>13 3 22</Arc>
    <Arc>2 4 13</Arc>
  </Arcs>
</MotionGraph>

这是我的代码:

public NodeValue nodes = null;
public ArrayList<NodeValue> motionNodePoints = new ArrayList<NodeValue>();
public ArcValue arcs = null;
public ArrayList<ArcValue> motionArcs = new ArrayList<ArcValue>();

   public class EnvironmentDataParse extends DefaultHandler{

    String TAG = "XMLHelper";

    Boolean currTag = false;
    String currTagVal = "";
    private Boolean inTag = false;

    public void get() {
        try {       
            String path = SharedValues.path;        
            File file = new File(path);
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser mSaxParser = factory.newSAXParser();
            XMLReader mXmlReader = mSaxParser.getXMLReader();
            mXmlReader.setContentHandler(this);
            mXmlReader.parse(new InputSource(new FileInputStream(file)));   
        } catch(Exception e) {
            Log.e(TAG, "Exception: " + e.getMessage());
        }
    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        if(currTag) {
            currTagVal = currTagVal + new String(ch, start, length);
            currTag = false;
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        currTag = false;

        if(localName.equalsIgnoreCase("Boundary"))
            boundary.setBoundary(currTagVal);


        else if(localName.equalsIgnoreCase("NodePoint"))
            nodes.setNodePoint(currTagVal);

        else if(localName.equalsIgnoreCase("Nodes"))
            motionNodePoints.add(nodes);

        else if(localName.equalsIgnoreCase("Arc"))
            arcs.setArc(currTagVal);

        else if(localName.equalsIgnoreCase("Arcs"))
            motionArcs.add(arcs);
    }

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        Log.i(TAG, "TAG: " + localName);

        currTag = true;
        currTagVal = "";
        if(localName.equals("head"))
            boundary = new BoundaryValue();

        else if(localName.equals("Space"))
            space = new SpaceValue();   

        else if(localName.equals("Nodes"))
            nodes = new NodeValue();

        else if(localName.equals("Arcs"))
            arcs = new ArcValue();
    }
}

和我的类NodeValue.java和ArcValue.java:

public class NodeValue implements Serializable{

private String nodePoint;

public String getNodePoint() {
    return nodePoint;
}

public void setNodePoint(String nodePoint) {
    this.nodePoint = nodePoint;
}

}

public class ArcValue implements Serializable{

private String arc;

public String getArc() {
    return arc;
}

public void setArc(String arc) {
    this.arc = arc;
}

}

我的问题是,当我使用代码时,我只获得最后一行NodePoint或Arc(所以只有3个数据),我希望在数组中包含所有3行(9个数据):

for (int i = 0; i < motionNodePoints.size(); i++) {
if (motionNodePoints.get(i).getNodePoint() == null) {}
else {
    float [] NodePoints = extractFloats(motionNodePoints.get(i).getNodePoint());
        for (int ii = 0; ii < NodePoints.length; ii ++) {
           Log.i("GEOMETRY", "ii " + ii);
           Log.i("GEOMETRY", "NODE POINT " + NodePoints[ii]);
        }
    }
}

有人能解释我做错了吗?

1 个答案:

答案 0 :(得分:1)

endElement方法中,您需要在找到<Arcs>标记的结尾时添加到列表中。您需要在创建要添加的元素后立即在<Arc>标记的末尾执行此操作。进行此更改后,该元素也可能不是一个字段,但可以是局部变量,可以使用endElement方法而不是startElement方法创建

同样适用于<Nodes><NodePoint>