Wrapping XML in CDATA

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • But I Haven't Eaten Any Sweetcorn!

    #1

    Wrapping XML in CDATA

    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

    --






  • Patrick TJ McPhee

    #2
    Re: Wrapping XML in CDATA

    In article <403391ce@baen1 673807.greenlnk .net>,
    But I Haven't Eaten Any Sweetcorn! <paul.x.lee@aes ystems.com> 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
    ptjm@interlog.c om

    Comment

    • But I Haven't Eaten Any Sweetcorn!

      #3
      Re: Wrapping XML in CDATA

      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(s ource);

      // inserting code goes here

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

      --

      Cheers
      Paul
      ------------------------------------------------
      Paul's Squalid World at



      "Patrick TJ McPhee" <ptjm@interlog. com> wrote in message
      news:c105nu$8vl $1@news.eusc.in ter.net...[color=blue]
      > In article <403391ce@baen1 673807.greenlnk .net>,
      > But I Haven't Eaten Any Sweetcorn! <paul.x.lee@aes ystems.com> 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[/color]
      is[color=blue]
      > % 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[/color]
      can't[color=blue]
      > % precede the
      > % root xml declaration in the 2nd XML structure, (and anyway, it gets[/color]
      cut[color=blue]
      > % 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[/color]
      generate[color=blue]
      > one when you serialise the included document.
      >
      > --
      >
      > Patrick TJ McPhee
      > East York Canada
      > ptjm@interlog.c om[/color]



      Comment

      • Dimitre Novatchev  [MVP XML]

        #4
        Re: Wrapping XML in CDATA

        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!" <paul.x.lee@bae systems.com> wrote in
        message news:403391ce@b aen1673807.gree nlnk.net...[color=blue]
        > 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
        >
        >
        >
        >
        >[/color]


        Comment

        • But I Haven't Eaten Any Sweetcorn!

          #5
          Re: Wrapping XML in CDATA

          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



          "Dimitre Novatchev [MVP XML]" <dnovatchev@yah oo.com> wrote in message
          news:c10hdd$1cu 18t$1@ID-152440.news.uni-berlin.de...[color=blue]
          > 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!" <paul.x.lee@bae systems.com> wrote[/color]
          in[color=blue]
          > message news:403391ce@b aen1673807.gree nlnk.net...[color=green]
          > > Hi all,
          > > I have two XML DOMs and I am trying to insert one into another, so[/color][/color]
          that[color=blue][color=green]
          > >
          > > <?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[/color][/color]
          is[color=blue][color=green]
          > > 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[/color][/color]
          can't[color=blue][color=green]
          > > precede the
          > > root xml declaration in the 2nd XML structure, (and anyway, it gets[/color][/color]
          cut[color=blue][color=green]
          > > out during
          > > the splice)
          > >
          > > Best wishes
          > >
          > > Paul
          > >
          > > --
          > > http://www.paullee.com
          > >
          > >
          > >
          > >
          > >[/color]
          >
          >[/color]



          Comment

          • Patrick TJ McPhee

            #6
            Re: Wrapping XML in CDATA

            In article <4033a2ad$1@bae n1673807.greenl nk.net>,
            But I Haven't Eaten Any Sweetcorn! <paul.x.lee@aes ystems.com> 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
            ptjm@interlog.c om

            Comment

            Working...