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

时间:2022-10-27 08:02:04

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. How I can read this value

在XSL的帮助下,我想检索cdata部分中存在的rno的值。我怎么能读到这个值

1 个解决方案

#1


0  

First of all, in your xml you should replace </student> by </Student> . Because XML tags are case sensitive reference here.

首先,在你的xml中你应该用 替换 。因为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 ;-)

希望能帮助到你 ;-)

#1


0  

First of all, in your xml you should replace </student> by </Student> . Because XML tags are case sensitive reference here.

首先,在你的xml中你应该用 替换 。因为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 ;-)

希望能帮助到你 ;-)