我的XML就像下面的
<?xml version="1.0" encoding="UTF-8"?>
<Students>
<![CDATA[<?xml version="1.0" encoding="UTF-8"?> <Student><rno>1</rno><name>xyz</name> </student>]]>
</Students>
在XSL的帮助下,我想检索cdata部分中存在的rno的值。我如何读取这个值
答案 0 :(得分:0)
首先,在您的xml中,您应该将</student>
替换为</Student>
。
因为XML标记区分大小写reference here。
自己做的诀窍如下:
public static String getRNO(){
String valueRetrieved = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
Document doc = null;
try {
db = dbf.newDocumentBuilder();
doc = db.parse(new File("D:\\Loic_Workspace\\Test2\\res\\test.xml"));
NodeList ndList = doc.getElementsByTagName("Students");
String xmlRetrieved = ndList.item(0).getTextContent();
if(xmlRetrieved != null) {
//CALL OF STRING REPLACE METHOD TO PREVENT FROM
//at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
//at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source
xmlRetrieved = xmlRetrieved.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "");
doc = db.parse(new InputSource(new StringReader(xmlRetrieved)));
valueRetrieved = doc.getElementsByTagName("rno").item(0).getTextContent();
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return valueRetrieved;
}
希望它有所帮助; - )