在我的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文件的类,我想要的是将id
和name
动态返回到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();
}
}
}