Xerces - How to read data in tag in CDATA section

Unable to parse the CDATA section in the input XML file.

Any Idea?


sample xml:
<code>

<script>
<![CDATA[
<message1> Welcome to TutorialsPoint 1 </message1>
<message2> Welcome to TutorialsPoint 2 </message2>
]] >
</script >

</code>

I want output like :
Welcome to TutorialsPoint 1
Welcome to TutorialsPoint 2


Please any help here will very help full.

Regards,
Akash Wagh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

string noTags( const string &input )
{
   string result;
   int inTag = 0;
   for ( char c : input )
   {
      if      ( c == '<' ) inTag++;
      else if ( c == '>' ) inTag--;
      else if ( !inTag   ) result += c;
   }
   return result;
}


string getSection( const string &input, const string &start, const string &finish )
{
   int p = input.find( start     );   if ( p == string::npos ) return "";
   p += start.size();
   int q = input.find( finish, p );   if ( q == string::npos ) return "";
   return input.substr( p, q - p );
}


int main()
{                                                                       
   string dataString( "<code>                                            \n"
                      "<script>                                          \n"
                      "<![CDATA[                                         \n"
                      "<message1> Welcome to TutorialsPoint 1 </message1>\n"
                      "<message2> Welcome to TutorialsPoint 2 </message2>\n"
                      "]] >                                              \n"
                      "</script >                                        \n"
                      "</code>                                           \n" );


   string CDATA = getSection( dataString, "![CDATA[", "]]" );
   stringstream ss( CDATA );
   for ( string line; getline( ss, line ); ) cout << noTags( line ) << '\n';
}


                                         
 Welcome to TutorialsPoint 1 
 Welcome to TutorialsPoint 2 



There are probably better ways with regex, but I'm not able to advise you on that.
Last edited on
Thank you lastchance for the reply.

I have implemented the below solution on similar line you have mentioned above.
Please have a look:

Traverse DOM tree till tag matches to the Parent Tag.
Then check for the children is of CDATA_SECTION_NODE

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
if(currentNode->getNodeType && if(currentNode->getNodeType == DOMNode:: CDATA_SECTION_NODE)
{
   DOMCharacterData* obj = (DOMCharacterData*) currentNode;
   const XMLCh* chardata = obj->getData();
   char* value = XMLString::transcode(chardata); 
   string x = value;
   int pos = c.find(tagname);
   
   // run a loop for multiple value with same tag
   while(pos > -1)
   {
      string tagValue;
      int posEnd = x.find("</TagName>", pos);
      int diff = posEnd - pos;
      tagValue = x.substr(pos, diff);

      // Update pos
      pos = x.find(tagName, posEnd);
   }
}
Last edited on
Topic archived. No new replies allowed.