• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Writing XML back to file; long elements are broken into two lines.  RSS feed

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am reading in an XML file, displaying the elements' values, and changing the elements' values with no problems.

Following some examples I found, I am using classes javax.xml.transform.Transformer and javax.xml.transform.stream.StreamResult to write the changed values back to the file.
This is working: the new values are in the file but I have one problem.

The values in my XML are strings and some of them are fairly long. When Transformer and StreamResult write them back to the file, they break the element into two lines in a way that I do not want:
The <data> nodes are third level nodes and are tabbed 3 times from the left edge; I couldn't figure out how to get this editor to keep those tabs except by putting in \t's; this worked but the \t's are visible: is there a better way to include XML?

original

\t\t\t <data>0. YOU ARE STANDING AT THE END OF A ROAD BEFORE A SMALL BRICK BUILDING.</data>
\t\t\t <data>AROUND YOU IS A FOREST. A SMALL STREAM FLOWS OUT OF THE BUILDING AND </data>
\t\t\t <data>DOWN A GULLY.</data>

becomes

\t\t\t <data>0. YOU ARE STANDING AT THE END OF A ROAD BEFORE A SMALL BRICK BUILDING.
</data>
\t\t\t <data>AROUND YOU IS A FOREST. A SMALL STREAM FLOWS OUT OF THE BUILDING AND
</data>
\t\t\t <data>DOWN A GULLY.</data>

Is there some way I can correct this so the becames is formatted the same as the original?
By the way, I changed
0. YOU ARE STANDING AT THE END OF A ROAD BEFORE A SMALL BRICK BUILDING.
to
When in the course of human events it becomes necessary for one people to dissolve
This string is longer than the original one that is causing me problems but it was written correctly to the file. ???

Here is the code that writes the XML back to the file.

Thanks.
 
Sheriff
Posts: 23714
50
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the identity transformer shouldn't insert newlines inside text elements in that way. So it must be that those text elements already have newlines at the end.

You mentioned that you were displaying and changing the elements' values -- perhaps that process is inserting the newline characters?
 
Rosie Fairfield
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I feel very foolish: the lines that are broken do indeed end in line feeds. (I think I didn't realize that this was the problem because I found that if I wrote the file using TinyXml and C++. it is written correctly.)

All of the data lines in a long description are concatenated into one string. I want the string to break at the end of each data element, hence the line feeds.

For example,
the first long description, for <chamber index="0"> should display as:
0. YOU ARE STANDING AT THE END OF A ROAD BEFORE A SMALL BRICK BUILDING.
AROUND YOU IS A FOREST. A SMALL STREAM FLOWS OUT OF THE BUILDING AND
DOWN A GULLY.

not as
0. YOU ARE STANDING AT THE END OF A ROAD BEFORE A SMALL BRICK BUILDING. AROUND YOU IS A FOREST. A SMALL STREAM FLOWS OUT OF THE BUILDING AND DOWN A GULLY.

I guess I'm going to have to remove the line feeds and add them when I read in the file.

Thanks for the help.
 
lowercase baba
Bartender
Posts: 12628
50
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rosie Fairfield wrote:
YOU ARE STANDING AT THE END OF A ROAD BEFORE A SMALL BRICK BUILDING. AROUND YOU IS A FOREST. A SMALL STREAM FLOWS OUT OF THE BUILDING AND DOWN A GULLY.


Enter Building
 
author
Sheriff
Posts: 23601
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hit troll with axe !!

 
Rosie Fairfield
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You guys have the right idea
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!