使用sax解析器根据id和name动态地将xml文件的id和名称返回到jsp页面

时间:2012-09-25 05:50:46

标签: java xml sax

在我的xml文件中,我在子提示符和音频中有一个id和一个name,我想根据id或名称获取文本值

<prompt id="p1" name="salesMsg"> details of sold Product invoice </prompt> 
<prompt id="p1" name="stockMsg"> closing stock </prompt> 
<prompt id="p1" name="ackMsg"> details of purchased Product invoice </prompt> 
<audio id="a1" name="salesMsg">eng/salesMsg.wav</audio> 
<audio id="a1" name="stockMsg">eng/stockMsg.wav</audio>

另外,我有一个用于解析上面的xml文件的类,我想要的是将idname动态返回到jsp文件。

所以,任何想法我怎么能这样做?

public class ClsPromptData {

    public static void main  (String argv[]) {

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

            DefaultHandler handler = new DefaultHandler()  {

                boolean bprompt = false;
                boolean baudio = false;

                StringBuffer sb = new StringBuffer();
                String id;
                String name;
                String prompt;
                String audio;

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

                    System.out.println("start element : " + qName);

                    if(qName.equalsIgnoreCase("prompt")) {

                        id = attributes.getValue("id");
                        name = attributes.getValue("name"); 

                        bprompt = true;
                    }

                    if(qName.equalsIgnoreCase("audio")){

                        id = attributes.getValue("id");
                        name = attributes.getValue("name");

                        baudio = true;
                    }
                }

                public void endElement (String uri, String localName, String qName) throws SAXException {   
                    System.out.println("End Element is :" + qName);
                }

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

                    if (bprompt) {
                        System.out.println("valueof id :" + id);
                        System.out.println("valueof name :" + name);
                        System.out.println("value of prompt :" + id +" and "+ name + new String(ch, start, length));
                        bprompt = false;
                    }

                    if(baudio) {
                        System.out.println("valueof id :" + id);
                        System.out.println("valueof name :" + name);
                        System.out.println("value of Audio :" + id +" and "+ name+ new String(ch, start, length));
                        baudio = false;
                    }
                }
            };

            String fileName = "promptSelect/Resources/GetProducts_eng.xml";
            InputStream prompt_file = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);

            saxParser.parse(prompt_file, handler);
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}

0 个答案:

没有答案