1

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

<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 文件,我想要的是将idandname动态返回到 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();
        }
    }
}
4

0 回答 0