user2748484
user2748484

Reputation: 123

Android: SAX parse XML with same tags

I want to parse with SAX this type of file:

<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>

Here is my code:

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();
    }
}

and my classes NodeValue.java and 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;
}

}

My problem is that when I use the code I obtain only the last line NodePoint or Arc (so only 3 data), and I want to have all the 3 lines (9 data) in an array:

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]);
        }
    }
}

Can somebody explain what I do wrong?

Upvotes: 0

Views: 329

Answers (1)

Don Roby
Don Roby

Reputation: 41137

In your endElement method, you're adding to the list on finding the end of the <Arcs> tag. You need to do it at the end of the <Arc> tag, immediately after creating the element you're adding. Once you make this change, that element also likely doesn't need to be a field, but can be a local variable, and can be created in the endElement method instead of in the startElement method.

Similarly for the <Nodes> and <NodePoint>.

Upvotes: 1

Related Questions