Wrapping XML in CDATA

  • Thread starter But I Haven't Eaten Any Sweetcorn!
  • Start date
B

But I Haven't Eaten Any Sweetcorn!

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
 
P

Patrick TJ McPhee

[...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.
 
B

But I Haven't Eaten Any Sweetcorn!

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!
 
B

But I Haven't Eaten Any Sweetcorn!

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 said:
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


message news:[email protected]...
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
 
P

Patrick TJ McPhee

% 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top