0

我正在使用 SAXParser 来解析 xml 文件。但是,我的应用程序仅解析我的 xml 文件中的前 3 项并忽略其余项。下面是我的代码和我要解析的 xml 文件:

主要活动:

public class XMLParsingExample extends Activity {

    /** Create Object For Bar Class */
    Bar barList = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        /** Create a new layout to display the view */
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(1);

        /** Create a new textview array to display the results */
        TextView colour[];
        TextView division[];
        TextView rect[];

        try {

            /** Handling XML */



            String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/saveObject/new.xml"; 
            File file = new File(path);
            SAXParserFactory parserFactory=SAXParserFactory.newInstance();
            SAXParser saxParser=parserFactory.newSAXParser();
            XMLReader reader=saxParser.getXMLReader();
            MyXMLHandler myXMLHandler = new MyXMLHandler();
            reader.setContentHandler(myXMLHandler);
            reader.parse(new InputSource(new FileInputStream(file)));



        } catch (Exception e) {
            System.out.println("XML Pasing Excpetion = " + e);
        }

        /** Get result from MyXMLHandler Bar Object */
        barList = MyXMLHandler.barList;

        /** Assign textview array lenght by arraylist size */
        colour = new TextView[barList.getColour().size()];
        division = new TextView[barList.getDivision().size()];
        rect = new TextView[barList.getRect().size()];

        /** Set the result text in textview and add it to layout */
        for (int i = 0; i < barList.getRect().size(); i++) {
            colour[i] = new TextView(this);
            colour[i].setText("Colour = "+barList.getColour().get(i));
            division[i] = new TextView(this);
            division[i].setText("Division = "+barList.getDivision().get(i));
            rect[i] = new TextView(this);
            rect[i].setText("Rect = "+barList.getRect().get(i));

            layout.addView(colour[i]);
            layout.addView(division[i]);
            layout.addView(rect[i]);
        }

        /** Set the layout view to display */
        setContentView(layout);

    }
}

XML 处理程序:

public class MyXMLHandler extends DefaultHandler {

    Boolean currentElement = false;
    String currentValue = null;
    public static Bar barList = null;

    public static Bar getbarList() {
        return barList;
    }

    public static void setbarList(Bar barList) {
        MyXMLHandler.barList = barList;
    }

    /** Called when tag starts*/

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

        currentElement = true;

        if (localName.equals("bar"))
        {
            /** Start */ 
            barList = new Bar();
        }

    }

    /** Called when tag closing*/
    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {

        currentElement = false;

        /** set value */ 
        if (localName.equalsIgnoreCase("colour"))
            barList.setColour(currentValue);
        else if (localName.equalsIgnoreCase("division"))
            barList.setDivision(currentValue);
        else if (localName.equalsIgnoreCase("rect"))
            barList.setRect(currentValue);



    }

    /** Called to get tag characters  */
    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {

        if (currentElement) {
            currentValue = new String(ch, start, length);

            currentElement = false;
        }

    }

}

酒吧班

public class Bar {
    /** Variables */
    private ArrayList<String> colour = new ArrayList<String>();
    private ArrayList<String> division = new ArrayList<String>();
    private ArrayList<String> rect = new ArrayList<String>();


    /** In Setter method default it will return arraylist 
     *  change that to add  */

    public ArrayList<String> getColour() {
        return colour;
    }

    public void setColour(String name) {
        this.colour.add(name);
    }

    public ArrayList<String> getDivision() {
        return division;
    }

    public void setDivision(String division) {
        this.division.add(division);
    }

    public ArrayList<String> getRect() {
        return rect;
    }

    public void setRect(String rect) {
        this.rect.add(rect);
    }

}

要解析的 XML 文件:

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<bar>
  <colour>Blue</colour>
  <division>8898</division>
  <rect>33</rect>
</bar>
<bar>
  <colour>Red</colour>
  <division>65</division>
  <rect>6876</rect>
</bar>
<bar>
  <colour>Black</colour>
  <division>65</division>
  <rect>6876</rect>
</bar>
<bar>
  <colour>Yellow</colour>
  <division>65</division>
  <rect>6876</rect>
</bar>
<bar>
  <colour>Red</colour>
  <division>65</division>
  <rect>6876</rect>
</bar>

谁能看到我哪里出错了?

4

1 回答 1

2

每次创建新柱实例时,您都会丢失旧柱实例。如果您尝试存储 Bar 实例的集合,请像这样更改 Bar 类:

public class Bar {
    private String colour;
    private String division;
    private String rect;

    public void setColor(String color) {
        this.color = color;
    }
    //other setters & getters...
}

在您的 MyXmlHandler 中,有一个 List 和一个当前 Bar:

public List<Bar> barList = new ArrayList<Bar>();
private Bar currentBar;

在 startElement 事件处理程序中,执行以下操作:

    if (localName.equals("bar"))
    {
        /** Start */ 
        currentBar = new Bar();
        barList.add(currentBar);
    }

在 endElement 事件处理程序中,

    if (localName.equalsIgnoreCase("colour"))
        currentBar.setColour(currentValue);
    else if (localName.equalsIgnoreCase("division"))
        currentBar.setDivision(currentValue);
    else if (localName.equalsIgnoreCase("rect"))
        currentBar.setRect(currentValue);

这应该可以解决问题,尽管我还没有测试过

于 2013-02-25T16:50:52.307 回答