Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How To Retain Entity Reference Character While Reading XML

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post How To Retain Entity Reference Character While Reading XML

    I have an xml file input.xml as below:
    <?xml version='1.0' encoding='UTF-8'?>
    <parameter name="&quot;name&quot;">
    	<summary>&quot;summary&quot;</summary>
    	<username>leon</username>
    </parameter>

    And I use dom4j library to parse the xml file to add email address postfix "@sa.com" to useranme element value:
    import org.dom4j.*;
    import org.dom4j.io.*;
    import java.io.*;
     
     
    public class Main {
     
    	public static void main(String[] args) throws Exception {
    	    SAXReader reader = new SAXReader();
    	    InputStream in = new FileInputStream("input.xml");
    	    Document doc = reader.read(in);
    	    Element element = (Element) doc.selectSingleNode("/parameter/username");
    	    element.setText(element.getText() + "@sa.com");
     
    	   StringWriter writer = new StringWriter();
    	   writer.write(doc.asXML());
     
    	   System.out.println("Converted Xml is:\n" + writer.toString());
    	}
     
    }

    After running the code, I get below result:
    Converted Xml is:
    <?xml version="1.0" encoding="UTF-8"?>
    <parameter name="&quot;name&quot;">
    	<summary>"summary"</summary>
    	<username>leon@sa.com</username>
    </parameter>

    From the result, you can see the summary element value changes from
    <summary>&quot;summary&quot;</summary>
    to
    <summary>"summary"</summary>
    But the name attribute value which contains entity reference character doesn't change.

    Why is it working like this? If I want to retain the original summary element value <summary>&quot;summary&quot;</summary>, how should I achieve that?


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: How To Retain Entity Reference Character While Reading XML

    Hello leon850515.

    Welcome to the Java Programming Forums.

    The only difference I can see between

    <parameter name="&quot;name&quot;">

    and

    <summary>&quot;summary&quot;</summary>

    Is that the first one is in quotes. I'm not sure if this makes a difference?!

    Try changing

    <summary>&quot;summary&quot;</summary>

    to

    <summary>"&quot;summary&quot;"</summary>

    and see what happens... This is just a test
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

Similar Threads

  1. Passing reference via object
    By Stefan_Lam in forum Java Theory & Questions
    Replies: 1
    Last Post: January 7th, 2011, 10:57 AM
  2. retain checked status of checkbox
    By rahulj5 in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: November 28th, 2010, 11:10 PM
  3. cannot find symbol in reference variable
    By NPotter86 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 1st, 2010, 08:21 PM
  4. Object as Reference not working
    By jassi in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 9th, 2010, 08:47 AM
  5. The character '' is an invalid XML character exception getting
    By sumanta in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 9th, 2010, 11:13 AM