Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用sax解析器根据id和名称动态地将xml文件的id和名称返回到jsp页面_Java_Xml_Sax - Fatal编程技术网

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

Java 使用sax解析器根据id和名称动态地将xml文件的id和名称返回到jsp页面,java,xml,sax,Java,Xml,Sax,在我的xml文件中,我在subchild提示符和音频中有一个id和一个名称,我想根据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"> de

在我的xml文件中,我在subchild提示符和音频中有一个id和一个名称,我想根据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>
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();
        }
    }
}