Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6714853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:32:38+00:00 2026-05-26T08:32:38+00:00

I’m working on a program that is using Dom4J to write xml files. The

  • 0

I’m working on a program that is using Dom4J to write xml files. The database schema I am writing to has a handy xml validation and import schema. Dom4J is working great, but, I can’t seem to figure out how to set the ‘preserve’ field in Dom4J’s XMLWriter class. I have a particular element where I need the encoded ‘\n’s preserved.

The javadoc for this class is a little underdocumented http://dom4j.sourceforge.net/dom4j-1.6.1/apidocs/org/dom4j/io/XMLWriter.html

I’ve tried playing around with on OutputFormat object but no dice.

Can anyone tell me how to ensure that an XMLWriter object preserves’s the whitespace of dom4j tree’s elements when writing to file.

Thanks,

Donald

Say I am starting with:

Element accession = factory.createElement("title"); 
List<String> AUT = new ArrayList<String>;
AUT.add("author1");
AUT.add("author2");
String title = "Title";

I would like to have an output similar to:

<title>author1
author2
Title</title>

With the line returns encoded into the title field.

DefaultEntity e = new DefaultEntity("#10");
if(AUT.size() > 1) {
  for(String a : AUT) {
    accession.addText(a);
    accession.add(e);
  }
accession.addText(title);
}

This does not work as it is an IllegalAddException.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:32:38+00:00Added an answer on May 26, 2026 at 8:32 am

    First of all, the “preserve” property has nothing to do with preserving the encoding of a previously encoded character–but rather with preserving the white space contained in an element. This property is usually controlled by the xml:space="preserve" attribute.

    However, if your use case is that you have an encoded newline in your input, that you want to be preserved in the output, you’re in trouble. DOM4J will decode all entities and character references to their corresponding Java characters (UTF-16). This is partially controllable by configuring the underlying XMLreader, but as far as i know, no XMLReader will report the start and end of character references–these will silently be replaced by their corresponding character values.

    On output, XMLWriter will encode only those characters that are required to be encoded, either because of the XML rules or because of the encoding used when serializeing (e.g. UTF-8 or ISO-8859-1, etc).

    In this case, you have basically two options.

    1) Sub class XMLWriter and completely replace the characters() method, since handling of white space is really intrinsic to this method. There is no other way that you can intercept the writing of tab, newline or carriage return. Here, you must somehow keep track of where you are and recognize that you’re processing the correct newline character

    2) Identify the new line character that you want to be “re-escaped” and replace that with a DefaultEntity("#10") node while setting the resolveEntityRefs property of XMLWriterto false. This option implies splitting an existing Text node in two and inserting the entity node in between.

    Seems like option 2 involves less work, while still being cumbersome

    UPDATE:

    OK, seems that you cannot add the same identical entity twice. If you add a new entity instance every time it works. However, your case could be fixed by àdding xml:space="preserve" to your element.

        if (AUT.size() > 1) {
            for (String a : AUT) {
                accession.addText(a);
                accession.addText("\n");
            }
            accession.addText(title);
        }
    

    and then

        accession.addAttribute(QName.get("space", Namespace.XML_NAMESPACE),
                "preserve");
    

    In this case, your explicitly added line breaks should be preserved, irregardless of the output format used when writing to xml.

    Sorry for the confusion.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I've got a string that has curly quotes in it. I'd like to replace
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.