Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Wrapping XML in CDATA

Reply
Thread Tools

Wrapping XML in CDATA

 
 
But I Haven't Eaten Any Sweetcorn!
Guest
Posts: n/a
 
      02-18-2004
Hi all,
I have two XML DOMs and I am trying to insert one into another, so that

<?xml = ....>
<Body>
<Head>
</Head>
</Body>

and

<?xml = ....>
<Eyes>
</Eyes>

Now becomes

<?xml = ....>
<Body>
<Head>
<Eyes>
</Eyes>
</Head>
</Body>

I have this working, with no problem...but what I would like to do is
produce the
following output instead:

<?xml = ....>
<Body>
<Head>
<![CDATA[
<Eyes>
</Eyes>
]]>
</Head>
</Body>

Is there anyway to do this? It seems to be tricky to do, as you can't
precede the
root xml declaration in the 2nd XML structure, (and anyway, it gets cut
out during
the splice)

Best wishes

Paul

--
http://www.paullee.com





 
Reply With Quote
 
 
 
 
Patrick TJ McPhee
Guest
Posts: n/a
 
      02-18-2004
In article <(E-Mail Removed)>,
But I Haven't Eaten Any Sweetcorn! <(E-Mail Removed)> wrote:

[...using DOM, currently combine two documents to become one:]

% <?xml = ....>
% <Body>
% <Head>
% <Eyes>
% </Eyes>
% </Head>
% </Body>
%
% I have this working, with no problem...but what I would like to do is
% produce the
% following output instead:
%
% <?xml = ....>
% <Body>
% <Head>
% <![CDATA[
% <Eyes>
% </Eyes>
% ]]>
% </Head>
% </Body>

% Is there anyway to do this?

You need to convert the inserted document to a string, then add that
to the containing document as the text of a CDATA node in the
appropriate spot.

% Is there anyway to do this? It seems to be tricky to do, as you can't
% precede the
% root xml declaration in the 2nd XML structure, (and anyway, it gets cut
% out during
% the splice)

In a CDATA section, you can include anything you want, except for `]]>'.
If you don't want text that looks like an xml declaration, don't generate
one when you serialise the included document.

--

Patrick TJ McPhee
East York Canada
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
But I Haven't Eaten Any Sweetcorn!
Guest
Posts: n/a
 
      02-18-2004
Hi Patrick,
I think I understand. Do you mean finding the correct spot in the insert
DOM,
add a CDATA tag, and then insert the second DOM.

I'm currently experimenting by, rather than parsing in an input file in
Java, reading it in as a string (without the opening "<?xml..." declaration)
and trying to work
on this

of course, the following statement:
InputSource source = new InputSource(new StringReader( "&lt;![CDATA[" +
inserted_text + "}}&gt;" ));

inserted_text = builder.parse(source);

// inserting code goes here

....doesn't work....
- which would be ideal!

--

Cheers
Paul
------------------------------------------------
Paul's Squalid World at
http://www.paullee.com


"Patrick TJ McPhee" <(E-Mail Removed)> wrote in message
news:c105nu$8vl$(E-Mail Removed)...
> In article <(E-Mail Removed)>,
> But I Haven't Eaten Any Sweetcorn! <(E-Mail Removed)> wrote:
>
> [...using DOM, currently combine two documents to become one:]
>
> % <?xml = ....>
> % <Body>
> % <Head>
> % <Eyes>
> % </Eyes>
> % </Head>
> % </Body>
> %
> % I have this working, with no problem...but what I would like to do

is
> % produce the
> % following output instead:
> %
> % <?xml = ....>
> % <Body>
> % <Head>
> % <![CDATA[
> % <Eyes>
> % </Eyes>
> % ]]>
> % </Head>
> % </Body>
>
> % Is there anyway to do this?
>
> You need to convert the inserted document to a string, then add that
> to the containing document as the text of a CDATA node in the
> appropriate spot.
>
> % Is there anyway to do this? It seems to be tricky to do, as you

can't
> % precede the
> % root xml declaration in the 2nd XML structure, (and anyway, it gets

cut
> % out during
> % the splice)
>
> In a CDATA section, you can include anything you want, except for `]]>'.
> If you don't want text that looks like an xml declaration, don't

generate
> one when you serialise the included document.
>
> --
>
> Patrick TJ McPhee
> East York Canada
> (E-Mail Removed)




 
Reply With Quote
 
Dimitre Novatchev [MVP XML]
Guest
Posts: n/a
 
      02-18-2004
People have argued that doing this is a bad thing.


Cheers,

Dimitre Novatchev [XML MVP],
FXSL developer, XML Insider,

http://fxsl.sourceforge.net/ -- the home of FXSL
Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html


"But I Haven't Eaten Any Sweetcorn!" <(E-Mail Removed)> wrote in
message news:(E-Mail Removed)...
> Hi all,
> I have two XML DOMs and I am trying to insert one into another, so that
>
> <?xml = ....>
> <Body>
> <Head>
> </Head>
> </Body>
>
> and
>
> <?xml = ....>
> <Eyes>
> </Eyes>
>
> Now becomes
>
> <?xml = ....>
> <Body>
> <Head>
> <Eyes>
> </Eyes>
> </Head>
> </Body>
>
> I have this working, with no problem...but what I would like to do is
> produce the
> following output instead:
>
> <?xml = ....>
> <Body>
> <Head>
> <![CDATA[
> <Eyes>
> </Eyes>
> ]]>
> </Head>
> </Body>
>
> Is there anyway to do this? It seems to be tricky to do, as you can't
> precede the
> root xml declaration in the 2nd XML structure, (and anyway, it gets cut
> out during
> the splice)
>
> Best wishes
>
> Paul
>
> --
> http://www.paullee.com
>
>
>
>
>



 
Reply With Quote
 
But I Haven't Eaten Any Sweetcorn!
Guest
Posts: n/a
 
      02-19-2004
Yes. Sadly I have no control over this, or doing it. It seems to be the
only
way to interface with legacy software!

--

Cheers
Paul
------------------------------------------------
Paul's Squalid World at
http://www.paullee.com


"Dimitre Novatchev [MVP XML]" <(E-Mail Removed)> wrote in message
news:c10hdd$1cu18t$(E-Mail Removed)-berlin.de...
> People have argued that doing this is a bad thing.
>
>
> Cheers,
>
> Dimitre Novatchev [XML MVP],
> FXSL developer, XML Insider,
>
> http://fxsl.sourceforge.net/ -- the home of FXSL
> Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html
>
>
> "But I Haven't Eaten Any Sweetcorn!" <(E-Mail Removed)> wrote

in
> message news:(E-Mail Removed)...
> > Hi all,
> > I have two XML DOMs and I am trying to insert one into another, so

that
> >
> > <?xml = ....>
> > <Body>
> > <Head>
> > </Head>
> > </Body>
> >
> > and
> >
> > <?xml = ....>
> > <Eyes>
> > </Eyes>
> >
> > Now becomes
> >
> > <?xml = ....>
> > <Body>
> > <Head>
> > <Eyes>
> > </Eyes>
> > </Head>
> > </Body>
> >
> > I have this working, with no problem...but what I would like to do

is
> > produce the
> > following output instead:
> >
> > <?xml = ....>
> > <Body>
> > <Head>
> > <![CDATA[
> > <Eyes>
> > </Eyes>
> > ]]>
> > </Head>
> > </Body>
> >
> > Is there anyway to do this? It seems to be tricky to do, as you

can't
> > precede the
> > root xml declaration in the 2nd XML structure, (and anyway, it gets

cut
> > out during
> > the splice)
> >
> > Best wishes
> >
> > Paul
> >
> > --
> > http://www.paullee.com
> >
> >
> >
> >
> >

>
>




 
Reply With Quote
 
Patrick TJ McPhee
Guest
Posts: n/a
 
      02-20-2004
In article <4033a2ad$(E-Mail Removed)>,
But I Haven't Eaten Any Sweetcorn! <(E-Mail Removed)> wrote:

% I think I understand. Do you mean finding the correct spot in the insert
% DOM,
% add a CDATA tag, and then insert the second DOM.

No, I mean that you convert the second DOM to a string, create
the CDATA node such that its content is that string, then insert the
CDATA node at the appropriate spot.

% I'm currently experimenting by, rather than parsing in an input file in
% Java, reading it in as a string (without the opening "<?xml..." declaration)
% and trying to work
% on this

If you're starting with a file rather than a DOM tree, then this is the way
to go. You might need to be careful about ]]> in the input, though.

% InputSource source = new InputSource(new StringReader( "&lt;![CDATA[" +
% inserted_text + "}}&gt;" ));

I don't see this as ideal. Once you start using an API like DOM, you should
use it. If you want to do the string manipulation yourself, then don't
use DOM for any of it -- find the spot in the text of the first file and
insert the appropriate text from the second. And don't use &lt; when you
want <. If you're using DOM, though, you need to create a CDATA node with
the appropriate content and insert it into the tree.
--

Patrick TJ McPhee
East York Canada
(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to retrieve XML CDATA text contents by org.xml.sax.ext.DefaultHandler2? RC Java 6 05-04-2009 11:33 PM
How to retrieve XML CDATA text contents by org.xml.sax.ext.DefaultHandler2? RC XML 6 05-04-2009 11:33 PM
Can I un-CDATA my CDATA section and elaborate a transformation for the contained data? troppfigo@excite.it XML 3 03-06-2006 03:01 AM
scriptalicious documentation suggests wrapping code in CDATA blocks - why? Jake Barnes Javascript 4 01-08-2006 12:36 AM
Extracting CDATA Text without CDATA Tags??? John Davison Java 1 07-06-2004 11:00 PM



Advertisments