1

我使用了下面的脚本,输出是
X 值:123 Y 值:123 X 值:123 Y 值:130

如何将第一个 X 设置为 X0;第一个 Y 作为 Y0,第二个 X 作为 X1,第二个 Y 作为 Y1?我使用了 SAX 解析器,它已经正确处理了我的输入文件,现在我想定义 X0、X1、Y0、Y1 以画线

public static void main(String argv[]) {

        try {

        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();

        DefaultHandler handler = new DefaultHandler() {

        boolean xele = false;
        boolean yele = false;


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

            if (qName.equalsIgnoreCase("X")) {
                xele = true;
            }

            if (qName.equalsIgnoreCase("Y")) {
                yele = true;
            }


        }
        public void characters(char ch[], int start, int length) throws SAXException {

            if (xele) {
                System.out.println("X value : " + new String(ch, start, length));
                xele = false;
            }

            if (yele) {
                System.out.println("Y value : " + new String(ch, start, length));
                yele = false;
            }


        }

         };

           saxParser.parse("c:\\input.xml", handler);

         } catch (Exception e) {
           e.printStackTrace();
         }

谢谢

4

1 回答 1

0

您可以在 DefaultHandler 实现中声明数组以保存 X 和 Y 的值以及在每次characters解析器调用方法时存储值后递增的索引,并通过 getter 公开这些数组。或者,您可以使用java.util.List/之类java.util.ArrayList的集合来保存值。

public static void main(String argv[])
{
    try
    {

        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();

        MyDefaultHandler handler = new MyDefaultHandler();
        saxParser.parse("c:\\input.xml", handler);
        System.out.println(handler.getXList() + ", " + handler.getYList());
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

static class MyDefaultHandler extends DefaultHandler
{
    private List<Integer> xList = new ArrayList<Integer>();
    private List<Integer> yList = new ArrayList<Integer>();
    boolean xele = false;
    boolean yele = false;

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

        if (qName.equalsIgnoreCase("X"))
        {
            xele = true;
        }

        if (qName.equalsIgnoreCase("Y"))
        {
            yele = true;
        }

    }

    public void characters(char ch[], int start, int length) throws SAXException
    {

        if (xele)
        {
            System.out.println("X value : " + new String(ch, start, length));
            xele = false;
            xList.add(Integer.parseInt(new String(ch, start, length)));
        }

        if (yele)
        {
            System.out.println("Y value : " + new String(ch, start, length));
            yele = false;
            yList.add(Integer.parseInt(new String(ch, start, length)));
        }
    }

    public List<Integer> getXList()
    {
        return xList;
    }

    public List<Integer> getYList()
    {
        return yList;
    }

};
于 2012-09-19T17:51:37.923 回答