Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Creating an XML file using JAVA

rjdfhorn_06rjdfhorn_06 Posts: 8Member
I've recently developed a JAVA program that reads in a .CSV (comma separated value) file, extracts relevant data, and outputs it into an XML file. Everything seems to work fine, but the indentation of the XML file isn't write. Can someone please help me with this? I am using the DOM APIs to create the XML file.

****************Here is what the XML file looks like after running the program*******************
Basically, everytime the program reads a new LINE, the root should display properly indented, but it only shows up twice, at the begin and the end. There should be multiple instances of this root element in the XML file.


<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
-
1659374882
1
BURACK
DAVID
A
DR.
MD
744 ARDEN LN
SUITE 225
ROCK HILL
SC
297322984
US
8033291598
8033294118
744 ARDEN LN
SUITE 225
ROCK HILL
SC
297322984
US
8033291598
8033294118
05/23/2005
06/10/2008
M
174400000X
39768
NC
N
174400000X
14896
SC
Y
SC148968
05
SC
2156835B
08
NC
C278814203
08
SC
19906
01
NC
BLUE CROSS BLUE SHIELD
C27881
02
1659374882
01
SC
BLUE CROSS BLUE SHIELD
NC8919906
05
NC
N
""



*****************This is my code:***********************
import java.util.*;
import java.io.*;

import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.TransformerFactory;
import org.w3c.dom.*;

public class HealthCareProviderIO {
public static void main(String[] args) throws Exception {
//File inputFile = new File("npidata_20050523-20090712.csv"); //File inputFile = new File("Test.csv");
File inputFile = new File("Health.csv");//test file
Scanner fileReader;
Boolean dataPresent;
//Boolean newBlock;
try{
fileReader = new Scanner(inputFile);
FileWriter fstream = new FileWriter("out.xml"); //create output file
BufferedWriter out = new BufferedWriter(fstream);
String root = "HealthCareProviderInformation";
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.newDocument();

Element rootElement = document.createElement(root);
document.appendChild(rootElement);

String categories="", element="", data="", fileLine="";
String[] fields = null;
String[] columnValues = null;

///////////////////////////////////////////////////////////////////////////
//extract each name of the fields defined on line 1; remove the quotations
categories = fileReader.nextLine();
fields = categories.split(",");
for(int y = 0; y < fields.length; y++){ //remove "" from categories
if(fields[y].length()>1){
fields[y] = fields[y].substring(1, fields[y].length()-1);
}
}
while(fileReader.hasNextLine()){
for(int iterator = 0; iterator <= fields.length-1; iterator++){
if(!fileReader.hasNextLine()){break;}
fileLine = fileReader.nextLine(); //retrieve line of data
//System.out.println(fileLine);
columnValues = fileLine.split(",");
if(columnValues[31].equalsIgnoreCase(""SC"")){//check if field 31 is from South Carolina
for(int y = 0; y < fields.length-1; y++){ //remove "" from columnValues
if(columnValues[y].length()>1){
columnValues[y] = columnValues[y].substring(1, columnValues[y].length()-1);
}
}
//field is from SC so manipulate LINE data
for(int counter = 0; counter < 314; counter++){
element = fields[counter];
String newEl = "";
for(int i = 0; i<element.length(); i++){
if(element.charAt(i)!=' ' && element.charAt(i)!='(' && element.charAt(i)!=')'){
newEl = newEl+element.charAt(i);
}
}
if(columnValues[counter].length() <= 0){
dataPresent = false;//no data
}
else{ //data present
dataPresent = true;
data = columnValues[counter];
}
if(dataPresent == true){
Element em = document.createElement(newEl);
//Element em = document.createElement(newEl + "HITHITHIT"); //+"HITHITHI" added on the end of the line
em.appendChild(document.createTextNode(data));//
rootElement.appendChild(em);
}
}
}
}
Source xmlSource = new DOMSource(document);
Result result = new StreamResult(out);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty("indent", "yes");
transformer.transform(xmlSource,result);
}
out.close();
fileReader.close();
}catch(FileNotFoundException fnfe){
System.out.println(fnfe.getMessage());
}
}
}
Sign In or Register to comment.