SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    wisconsin
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    DOM TextNode special characters

    I have created Text object using the org.w3c.dom.Text. After appending to the Document object, special characters such as <>"' will be translated into &lt; &gt; etc. Is there any way to escape the special characters? So I can get <> instead of &lt; &gt;

    Thanks

  2. #2
    SitePoint Zealot
    Join Date
    May 2005
    Location
    Mud people of Worksop
    Posts
    181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <> need to be changed into the entities when encoding otherwise your xml file would be invalid. When you get the data back out your processor should convert them back into the <> characters you want.

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    wisconsin
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sks
    <> need to be changed into the entities when encoding otherwise your xml file would be invalid.
    I'm not sure what you meant by the entites? Can you provide an example?

    Here is what I have:

    Code:
    Element jsElement = newDoc.createElement( "script" );
    Text javascriptText = newDoc.createTextNode
            ( "<!-- function test(){ alert('test'); } --> " );
    
    jsElement.appendChild( javascriptText );
    parentNode.appendChild( javascriptText );
    When I output the parentNode, the text node value will be converted to
    &lt;!-- function test(){ alert('test'); } --&gt;

    how would I get the parentNode to show:
    "<!-- function test(){ alert('test'); } -->

  4. #4
    SitePoint Zealot
    Join Date
    May 2005
    Location
    Mud people of Worksop
    Posts
    181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well I only ever use JDOM so I'm not an expert, but it seems createTextNode is creating a new Node, with either that as the name, or that as the content. Either way, it has to be encoded otherwise your < and > will clash with the <> of the actual element.

    Ie,

    <Root>
    <Element>This is my element value with <> which will confuse a parser</Element>
    </Root>

    Instead it outputs

    <Root>
    <Element>This is my element value with &lt;&gt; which will not confuse a parser</Element>
    </Root>

  5. #5
    SitePoint Zealot
    Join Date
    May 2005
    Location
    Mud people of Worksop
    Posts
    181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What's this got to do with Javascript (which is where the thread has been moved!)

  6. #6
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    wisconsin
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sks
    What's this got to do with Javascript (which is where the thread has been moved!)
    This has nothing to do with javascript. It just happens that my text node includes javascript text. My question is related to the Java DOM object.

    thanks for your help sks!

  7. #7
    SitePoint Zealot
    Join Date
    May 2005
    Location
    Mud people of Worksop
    Posts
    181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Did you get your problem sorted?

  8. #8
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    wisconsin
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the problem is solved.

    thanks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •