Generating 8000 text files from xml files

Posted by Ray on Stack Overflow See other posts from Stack Overflow or by Ray
Published on 2010-06-02T01:56:09Z Indexed on 2010/06/02 2:03 UTC
Read the original article Hit count: 252

Filed under:
|
|
|
|

Hi all, i need to generate the same number of text files as the xml files i have. Within the text files, i need the title and maybe some other tags of it. I can generate text files with the elements i wanted but not all xml files can be generated. Only some of them are generated. Something might be wrong with my parser so help out please thanks.

This is my code. Please have a look and give me suggestions. Thanks in advance.

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.*;
import java.io.*;

public class AccessingXmlFile1 {

 public static void main(String argv[]) {

  try {
  //File file = new File("C:\\MyFile.xml");

    // create a file that is really a directory
    File aDirectory = new File("C:/Documents and Settings/I2R/Desktop/test");

    // get a listing of all files in the directory
    String[] filesInDir = aDirectory.list();

    System.out.println(""+filesInDir.length);

    // sort the list of files (optional)
    // Arrays.sort(filesInDir);

  ////////////////////////////////////////////////////////////////////////////////////

  ////////////////////////////////////////////////////////////////////////////////////

    // have everything i need, just print it now
  for ( int a=0; a<filesInDir.length; a++ )
  {     
  String xmlFile = filesInDir[a];
  String newLine = System.getProperty("line.separator");

    File file = new File(xmlFile);
  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  DocumentBuilder db = dbf.newDocumentBuilder();
  Document document = db.parse(file);
  document.getDocumentElement().normalize();
  //System.out.println("Root element " + document.getDocumentElement().getNodeName());
  NodeList node = document.getElementsByTagName("metadata");
  System.out.println("Information of Xml File");
  System.out.println(xmlFile.substring(0, xmlFile.length() - 4));
    ////////////////////////////////////////////////////////////////////////////////////
    String titleStoreText = "";
    String descriptionStoreText = "";
    String collectionStoreText = "";
    String textToWrite = "";
    ////////////////////////////////////////////////////////////////////////////////////
  for (int i = 0; i < node.getLength(); i++) {
  Node firstNode = node.item(i);

    if (firstNode.getNodeType() == Node.ELEMENT_NODE) {

      Element element = (Element) firstNode;
      NodeList titleElementList = element.getElementsByTagName("title");
      Element titleElement = (Element) titleElementList.item(0);
      NodeList title = titleElement.getChildNodes();
      ////////////////////////////////////////////////////////////////////////////////////
      if(titleElement == null)
            titleStoreText = " There is no title for this file."+ newLine; 
            else 
            titleStoreText = titleStoreText+((Node) title.item(0)).getNodeValue() + newLine;

      //titleStoreText = titleStoreText+((Node) title.item(0)).getNodeValue()+ newLine;
      ////////////////////////////////////////////////////////////////////////////////////
      System.out.println("Title : " + titleStoreText);

        NodeList collectionElementList = element.getElementsByTagName("collection");
      Element collectionElement = (Element) collectionElementList.item(0);
      NodeList collection = collectionElement.getChildNodes();
      ////////////////////////////////////////////////////////////////////////////////////
      if(collectionElement == null)
            collectionStoreText = " There is no collection for this file."+ newLine; 
            else 
            collectionStoreText = collectionStoreText+((Node) collection.item(0)).getNodeValue() + newLine;

      //collectionStoreText = collectionStoreText+((Node) collection.item(0)).getNodeValue()+ newLine;
      ////////////////////////////////////////////////////////////////////////////////////
      System.out.println("Collection : " + collectionStoreText);

            NodeList descriptionElementList = element.getElementsByTagName("description");
      Element descriptionElement = (Element) descriptionElementList.item(0);
      NodeList description = descriptionElement.getChildNodes();
      ////////////////////////////////////////////////////////////////////////////////////

      if(descriptionElement == null)
            descriptionStoreText = " There is no description for this file."+ newLine; 
            else 
            descriptionStoreText = descriptionStoreText+((Node) description.item(0)).getNodeValue() + newLine;


      //descriptionStoreText = descriptionStoreText+((Node) description.item(0)).getNodeValue() + newLine;
      ////////////////////////////////////////////////////////////////////////////////////
      System.out.println("Description : " + descriptionStoreText);

      ////////////////////////////////////////////////////////////////////////////////////
      textToWrite = "=====Title=====" + newLine + titleStoreText + newLine + "=====Collection=====" + newLine + collectionStoreText + newLine + "=====Description=====" + newLine + descriptionStoreText;// + newLine + "=====Subject=====" + newLine + subjectStoreText;
      ////////////////////////////////////////////////////////////////////////////////////
 }

}
///////////////////////////////////////////write to file part is here/////////////////////////////////////////

        Writer output = null;

    File file2 = new File(xmlFile.substring(0, xmlFile.length() - 4)+".txt");
    output = new BufferedWriter(new FileWriter(file2));
    output.write(textToWrite);
    output.close();
    System.out.println("Your file has been written"); 
        ////////////////////////////////////////////////////////////////////////////////////
  }


  } catch (Exception e) {
    e.printStackTrace();
  }
 }
} 

© Stack Overflow or respective owner

Related posts about Xml

Related posts about text