Search code examples
javaxmlstringstringbuffer

Read XML file lines and read them in as strings in java.


I have following file with xml data in it. I don't want to parse this data and want to put that information into a Java String or StringBuffer.

<Node number="5"> 

<line/> 
<line center="1">** DATA **</line> 
<line/> <line/> 
<line>Data1: %Data%</line> 
<line>Data2 : %Data%</line> 
<line/> 
<line align="left">%DATA%</line> 
<line align="right">%DATA%</line> 
<line/> <line/> 
</Node>

I want that xml data in a java data structure as-is so I can manipulate the lines, what structure should I use and how should this be done?


Solution

  • If I'm understanding what you're looking for, Files.readAllLines is the simplest solution (if Java 7 is available). Create a Path pointing to your file, and call readAllLines with it and the character set of the file as parameters to get a List of Strings.