2016-04-04 20 views
0

XMLを出力するファイルを取得できました。しかし何らかの理由で、parentLocalityNameに要素が存在しない場合、終了タグだけが表示されます。これは、終了タグだけが表示されるためです。どのように私は、全体のタグが表示されるように自分のコードを変更するが、それの中に何もない?DOMを使用してCSVファイルを正しく解析するにはどうすればよいですか?

-<BusStopDetails> 

<AtcoCode>0800COC31523</AtcoCode> 

<CommonName>Bus Station</CommonName> 

<LocalityName>Newquay</LocalityName> 

<ParentLocalityName/> 

<Latitude>50.4130339395</Latitude> 

<Longitude>-5.0856695446</Longitude> 

</BusStopDetails> 

これは、ファイルを変換し、私のJavaプログラムです:

import java.io.*; 
import java.util.*; 
import org.w3c.dom.*; 
import javax.xml.parsers.*; 
import javax.xml.transform.*; 
import javax.xml.transform.dom.DOMSource; 
import javax.xml.transform.stream.StreamResult; 
import java.io.File; 

public class CSV2XML { 

    // Protected Properties 
    protected DocumentBuilderFactory domFactory = null; 
    protected DocumentBuilder domBuilder = null; 

    // CTOR 
    public CSV2XML() { 
     try { 
      domFactory = DocumentBuilderFactory.newInstance(); 
      domBuilder = domFactory.newDocumentBuilder(); 
     } catch (FactoryConfigurationError exp) { 
      System.err.println(exp.toString()); 
     } catch (ParserConfigurationException exp) { 
      System.err.println(exp.toString()); 
     } catch (Exception exp) { 
      System.err.println(exp.toString()); 
     } 
    } 

    @SuppressWarnings("resource") 
    public static void main(String[] args) throws IOException, TransformerException { 
     ArrayList<String> busStopInfo = new ArrayList<String>(7); 
     // An array list has been used to store the elements from the csv file. They are stored as strings. 
     try { 

      File file = new File("C:\\Users\\liaml\\OneDrive\\Documents\\CSCU9T4 XML assignment\\lrl00002\\stops.csv"); 
      BufferedReader readFile = null; 

      DocumentBuilderFactory df = DocumentBuilderFactory.newInstance(); 
      DocumentBuilder db; 

      db = df.newDocumentBuilder(); 

      Document doc = db.newDocument(); 

      // Root element 
      Element rootElement = doc.createElement("BusStops"); // root element "Busstops created" 

      doc.appendChild(rootElement); 
      readFile = new BufferedReader(new FileReader(file)); 
      int line = 0; // Represent the lines in the file, starts at the 0th, 
          // increments after every line has been tokenised 
          // for elements line of the file 

      String information = null; 
      while ((information = readFile.readLine()) != null) { 

       String[] tokens = information.split(","); // removes comma until there is no more commas 

       String[] row = information.split(","); // store elements after the command, length 6 to store headers 



       if (line == 0) { 
        for (String column : row) { 
         busStopInfo.add(column); // This will add column headers from rowValues to busStopInfo ArrayList 

        } 
       } else { 
        Element childElement = doc.createElement("BusStopDetails"); // creates child element details 

        rootElement.appendChild(childElement); 
        for (int column = 0; column < busStopInfo.size(); column++) { 

         String header = busStopInfo.get(column); 
         String value = null; 

         if (column < row.length) { 
          value = row[column]; 
         } else { 
          value = " "; 
         } 

         Element current = doc.createElement(header); // creates element of current header 

         current.appendChild(doc.createTextNode(value)); // creates placement for value 

         childElement.appendChild(current); // adds current value of the header into child element details 


         // Save the document to the disk file 
         TransformerFactory tranFactory = TransformerFactory.newInstance(); 
         Transformer aTransformer = tranFactory.newTransformer(); 
         aTransformer.setOutputProperty(OutputKeys.INDENT, "yes"); 
         aTransformer.setOutputProperty(OutputKeys.METHOD, "xml"); 
         aTransformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); 
         Source src = new DOMSource(doc); 
         information = ("C:\\Users\\liaml\\OneDrive\\Documents\\CSCU9T4 XML assignment\\lrl00002\\stops.xml"); 
         Result dest = new StreamResult(new File(information)); 
         aTransformer.transform(src, dest); 


         if (line >= 0) { 
          System.out.println("CSV File has been successfully converted to XML File & Stored in Zip Folder " 
            + "(" + String.valueOf(line) + " row)"); 
         } else { 
          System.out.println(
            "Error while converting input CSV File " + args[0] + " to output XML File " + args[1] + " "); 
         } 

        } 
       } 
       line++; 
      } 
     } catch (ParserConfigurationException e) { 
      e.printStackTrace(); 
     } 

    } 
} 

答えて

1

<ParentLocalityName/>タグです。
</ParentLocalityName>は、エンドタグです。
同じことではありません。

空タグ(see XML spec)は、開始タグと終了タグの省略形です。

<ParentLocalityName></ParentLocalityName> 
<ParentLocalityName/> 

上記の2行はまったく同じことを意味し、一度解析されると区別することはできません。

はところで:あなたのタイトルは偽です:あなたは解析 CSVは DOMを使用してを提出することはできません。
あなたはCSVファイルを読んでおり、 DOMを使ってXMLを書いています。

関連する問題