Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

java xml parsing

  • 08-08-2009 09:03PM
    #1
    Registered Users, Registered Users 2 Posts: 134 ✭✭


    i'm trying to create an xml file using java but am having a problem with escape character. This is the code below:

    //add rule ref
    Element ruleref = document.createElement("ruleref");
    ruleref.setAttribute("uri", "#_"+MYDATA);
    PublicItem.appendChild(ruleref);

    //add tag
    Element tag = document.createElement("tag");
    PublicItem.appendChild(tag);
    //delegate the tag "MYDATA" value from the private rule
    String tagVal = "<![CDATA[MYDATA = "+ "\"_"+varData+".MYDATA\"; "+
    "dm_confirmation_mode=\"never\"; "+
    "crResultType=\"rSlot\"; "+
    "dm_root=\""+ MYDATA+ "\"" +
    "]]>";

    The problem is that when the document is created the '<' and '>' characters that exists within the the <tag> element are encoded as < and > and this results in the grammar not being valid.

    see output below for the current output:
    <tag><![CDATA[ MYDATA = "_111111.MYDATA"; dm_confirmation_mode="never"; rSlot="Speech"; dm_root="MYDATA"]]></tag>

    once the document is created i can then import the doc as an input stream and do a string replace to change the characters but this is not very elegant and has a significant overhead as some of the files are quite large.

    is there any way i can ensure these characters are not encoded when i first create the document? I am already using the Transformer class before I write to the output file first.


Comments

  • Registered Users, Registered Users 2 Posts: 1,922 ✭✭✭ronivek


    Try the following;
    ...
    CDATASection cDataSection = document.createCDATASection(myData);
    tag.appendChild(cDataNode);
    ...
    

    I'm not 100% sure if that'll work but I reckon it should.


  • Registered Users, Registered Users 2 Posts: 134 ✭✭d4v1d


    rock n' roll!

    worked a treat. thank you.


Advertisement