简体   繁体   English

如何使用xsl从cdata部分检索特定的标签值

[英]How to retrievea particular tag value from the cdata section using xsl

My XML is like bellow 我的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>

with the help of XSL i want to retrieve the value of rno which is present inside cdata section. 借助XSL,我想检索cdata节中存在的rno的值。 How I can read this value 我如何读取此值

First of all, in your xml you should replace </student> by </Student> . 首先,在xml中,您应将</student>替换为</Student> Because XML tags are case sensitive reference here . 因为XML标记在此处区分大小写。

The trick to do that on your own is the following : 自行完成此操作的技巧如下:

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;

}

Hope it helps ;-) 希望能帮助到你 ;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM