Wrapping XML in CDATA

Discussion in 'XML' started by But I Haven't Eaten Any Sweetcorn!, Feb 18, 2004.

  1. 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
     
    But I Haven't Eaten Any Sweetcorn!, Feb 18, 2004
    #1
    1. Advertising

  2. In article <>,
    But I Haven't Eaten Any Sweetcorn! <> 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
     
    Patrick TJ McPhee, Feb 18, 2004
    #2
    1. Advertising

  3. 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" <> wrote in message
    news:c105nu$8vl$...
    > In article <>,
    > But I Haven't Eaten Any Sweetcorn! <> 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
    >
     
    But I Haven't Eaten Any Sweetcorn!, Feb 18, 2004
    #3
  4. 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!" <> wrote in
    message news:...
    > 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
    >
    >
    >
    >
    >
     
    Dimitre Novatchev [MVP XML], Feb 18, 2004
    #4
  5. 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]" <> wrote in message
    news:c10hdd$1cu18t$-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!" <> wrote

    in
    > message news:...
    > > 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
    > >
    > >
    > >
    > >
    > >

    >
    >
     
    But I Haven't Eaten Any Sweetcorn!, Feb 19, 2004
    #5
  6. In article <4033a2ad$>,
    But I Haven't Eaten Any Sweetcorn! <> 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
     
    Patrick TJ McPhee, Feb 20, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Davison
    Replies:
    1
    Views:
    584
    Hal Rosser
    Jul 7, 2004
  2. Replies:
    3
    Views:
    755
    Joe Kesselman
    Mar 6, 2006
  3. RC
    Replies:
    6
    Views:
    607
  4. RC
    Replies:
    6
    Views:
    3,739
  5. Jake Barnes
    Replies:
    4
    Views:
    116
    Thomas 'PointedEars' Lahn
    Jan 8, 2006
Loading...

Share This Page