0

I am trying to retrieve CDATA value in JDOM2 using getText() but I only get the following -
<![CDATA[ ]]>

My XML looks like below

<ROOT>
 <CHILD>
    <P><![CDATA[<ROOT><ELEMENT>SOMECONTENT</ELEMENT></ROOT>]]></P>
 </CHILD>
</ROOT>

the CDATA contains XML content which I need as string to store it elsewejhere Code(snippet) looks like below

XPathFactory xpfac = XPathFactory.instance();
XPathExpression<Element> xElements = xpfac.compile(sXpath,Filters.element(),null,Namespace.getNamespace("myns", "http://www.namespace.com/ns"));
List<Element> elements = xElements.evaluate(doc);       
for (Element xElem : elements) {
    if(!isCDATA)
    {
        sRetval=xElem.getValue();
    }
    else
    {
        sRetval=xElem.getText();
    }
    return sRetval;
}

getValue() works fine whereas getText() returns only <![CDATA[ ]]>

of course my XPAHT looks like this //ROOT/CHILD/P Any idea how I could get the content of CDATA?

Edit: If I repalce <P><![CDATA[ SOMECONTENT]]></P> I am getting "SOMECONTENT"..So I am unable to get the XML content from CDATA

KK99
  • 1,971
  • 7
  • 31
  • 64
  • Why retrieve the `text` of the `CDATA` node? It seems like it should be sufficient to just get the `text` or `value` of the containing `

    ` tag.

    – millimoose Apr 14 '13 at 19:47
  • And if `getValue()` works fine why not just use that? – millimoose Apr 14 '13 at 19:48
  • `getValue()` isn't returning the whole cdata but only the TEXT content from CDATA (without the tags) Min my case – KK99 Apr 15 '13 at 04:16

1 Answers1

2

I have tried to reproduce your problem, but can't..... (posted as an answer to get formatting right)....

public static void main(String[] args) throws JDOMException, IOException {
    final String xml = "<ROOT>\n <CHILD>\n    <P><![CDATA[<ROOT><ELEMENT>SOMECONTENT</ELEMENT></ROOT>]]></P>\n </CHILD>\n</ROOT>";

    Document doc = new SAXBuilder().build(new StringReader(xml));
    XPathFactory fac = XPathFactory.instance();
    XPathExpression<Element> ex = fac.compile("//ROOT/CHILD/P", Filters.element());
    for (Element e : ex.evaluate(doc)) {
        System.out.printf("getValue() %s\n", e.getValue());
        System.out.printf("getText() %s\n", e.getText());
    }
}

produces:

getValue() <ROOT><ELEMENT>SOMECONTENT</ELEMENT></ROOT>
getText() <ROOT><ELEMENT>SOMECONTENT</ELEMENT></ROOT>

What am I doing differently to you?

rolfl
  • 17,539
  • 7
  • 42
  • 76
  • Thanks for posting an answer. What I get when I do getValue() is the textual content from inner xml without the nodes..example I get `SOMECONTENT` and not `SOMECONTENT`. After sturggling with that for 2 days I started exploring other methods but in vain. I am using JDOM 2.0.4. The difference I see with my code is, I have a namespace declaration and you don't but I don't think that matters because I get textual content alone when I use getValue() I get some content but not all (the elements itself) `e.getText()` returns me this `<![CDATA[]]>` – KK99 Apr 15 '13 at 04:16
  • 1
    OK, so the difference is that the input XML, input XPath, and namespaces are all different to what you have posted here... that's not a *small* challenge. Please update your question to reflect the real state of your problem. Also, preferably, post an issue on the JDOM GitHub https://github.com/hunterhacker/jdom/issues/new with a more realistic representaion of your problem. I was planning on releasing JDOM 2.0.5 today, but if you have a real problem, I will postpone until it is resolved. – rolfl Apr 15 '13 at 10:32
  • Thanks @rolfl I have created an Issue([link]https://github.com/hunterhacker/jdom/issues/117) but without any XML sample as I have to think about the data in that. In worst case I have to prepare a XML. Like I mentioned here, I am able to get the CDATA contents in C# using System.XML without any issues. Please tell me if you need more info – KK99 Apr 15 '13 at 11:49
  • HI @rolfl I have posted a sample source code which can reproduce the behavior to the issue in github https://github.com/hunterhacker/jdom/issues/117 – KK99 May 27 '13 at 09:50
  • I hav added a comment to the same issue. The underlying problem is that the transformation process is adding special transformation processing instructions to the XML output from the transform, andJDOM is using those special instructions to output the XML. These instructions by default make the output XML from JDOM different to the output XML from the transform, which in turn is leading to your confusion. – rolfl May 27 '13 at 12:35