I am working on a project that is suppost to work with 3 xml files. The program is suppost to ask which file you would like to import. It then makes a new folder in the current directory. The program is supposed to create a new file in the created folder for each record in the xml file. It needs to be scalable so it will work even if more records are added to the xml file. The new files should be named Chappel_R_a1Bx.txt, with x being the number of records. For example, Chappel_R.a1B1 is the file for the first record, Chappel_R_a1B2 is for the second record, etc. I have gotten as far as creating a new directory, but am not sure where to go from there. Also, the program must be able to run on linux command line.

import java.io.*;
import java.io.File;
import java.util.Scanner;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Chappel_R_a1B 
{
 public static void main(String argv[])
 {//start main
 //----ask user for name of file they wish to input, used Data Structures and Abstractions with Java texbook by Frank M. Carrano as reference for this----//
     Scanner scanner = new Scanner (System.in);
     System.out.println("Please type in the name of the file you wish to import ('cd_catalog.xml', 'simple.xml', or 'plant_catalog.xml')");
     String fileName = scanner.next();

     //---------------Handles if file chosen is cd_catalog.xml---------------//

     if (fileName.equals("cd_catalog.xml"))
     {//start if
     //--------Create new folder in current directory,used mkyong from http://www.mkyong.com/java/how-to-create-directory-in-java/ as a source----------//
        File direc = new File("Records from cd_catalog.xml");
        if (!direc.exists()) 
        {//start if
            if (direc.mkdir()) 
            {//start if
                System.out.println("Directory is created!");
                    try
                    {//start try

                        File file = new File("cd_catalog.xml"); //imports cd_catalog.xml
                        BufferedWriter outFile=new BufferedWriter(new FileWriter("Records from cd_catalog.xml/test.txt")); // test creates new .txt in current directory
                        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                       DocumentBuilder db = dbf.newDocumentBuilder();
                        Document doc = db.parse(file);
                        doc.getDocumentElement().normalize();
                        NodeList nodeLst = doc.getElementsByTagName("CD");



                    }//end try
                    catch (Exception e) 
                    {//start catch
                     e.printStackTrace();
                    }//end catch
            } //end if
            else 
            {//start else
                System.out.println("Failed to create directory!");
            }//end else
        }//end if


     }//end if

     //------------------------handles if file chosen is simple.xml----------------------//
     if (fileName.equals("simple.xml"))
     {//start if

        //will just copy and paste code above into here and change tag names
     }//end if

     //--------------------handles if file chosen is plant_catalog.xml-----------------------//
     if (fileName.equals("plant_catalog.xml"))
     {//start if

        //will copy above code into here and change tag names to match relevent file

     }//end if

 }//End main
}//end class

Here is the cd_catalog.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<CATALOG>
    <CD>
        <TITLE>Empire Burlesque</TITLE>
        <ARTIST>Bob Dylan</ARTIST>
        <COUNTRY>USA</COUNTRY>
        <COMPANY>Columbia</COMPANY>
        <PRICE>10.90</PRICE>
        <YEAR>1985</YEAR>
    </CD>
    <CD>
        <TITLE>Hide your heart</TITLE>
        <ARTIST>Bonnie Tyler</ARTIST>
        <COUNTRY>UK</COUNTRY>
        <COMPANY>CBS Records</COMPANY>
        <PRICE>9.90</PRICE>
        <YEAR>1988</YEAR>
    </CD>
    <CD>
        <TITLE>Greatest Hits</TITLE>
        <ARTIST>Dolly Parton</ARTIST>
        <COUNTRY>USA</COUNTRY>
        <COMPANY>RCA</COMPANY>
        <PRICE>9.90</PRICE>
        <YEAR>1982</YEAR>
    </CD>
    <CD>
        <TITLE>Still got the blues</TITLE>
        <ARTIST>Gary Moore</ARTIST>
        <COUNTRY>UK</COUNTRY>
        <COMPANY>Virgin records</COMPANY>
        <PRICE>10.20</PRICE>
        <YEAR>1990</YEAR>
    </CD>
    <CD>
        <TITLE>Eros</TITLE>
        <ARTIST>Eros Ramazzotti</ARTIST>
        <COUNTRY>EU</COUNTRY>
        <COMPANY>BMG</COMPANY>
        <PRICE>9.90</PRICE>
        <YEAR>1997</YEAR>
    </CD>
    <CD>
        <TITLE>One night only</TITLE>
        <ARTIST>Bee Gees</ARTIST>
        <COUNTRY>UK</COUNTRY>
        <COMPANY>Polydor</COMPANY>
        <PRICE>10.90</PRICE>
        <YEAR>1998</YEAR>
    </CD>
    <CD>
        <TITLE>Sylvias Mother</TITLE>
        <ARTIST>Dr.Hook</ARTIST>
        <COUNTRY>UK</COUNTRY>
        <COMPANY>CBS</COMPANY>
        <PRICE>8.10</PRICE>
        <YEAR>1973</YEAR>
    </CD>
    <CD>
        <TITLE>Maggie May</TITLE>
        <ARTIST>Rod Stewart</ARTIST>
        <COUNTRY>UK</COUNTRY>
        <COMPANY>Pickwick</COMPANY>
        <PRICE>8.50</PRICE>
        <YEAR>1990</YEAR>
    </CD>
    <CD>
        <TITLE>Romanza</TITLE>
        <ARTIST>Andrea Bocelli</ARTIST>
        <COUNTRY>EU</COUNTRY>
        <COMPANY>Polydor</COMPANY>
        <PRICE>10.80</PRICE>
        <YEAR>1996</YEAR>
    </CD>
    <CD>
        <TITLE>When a man loves a woman</TITLE>
        <ARTIST>Percy Sledge</ARTIST>
        <COUNTRY>USA</COUNTRY>
        <COMPANY>Atlantic</COMPANY>
        <PRICE>8.70</PRICE>
        <YEAR>1987</YEAR>
    </CD>
    <CD>
        <TITLE>Black angel</TITLE>
        <ARTIST>Savage Rose</ARTIST>
        <COUNTRY>EU</COUNTRY>
        <COMPANY>Mega</COMPANY>
        <PRICE>10.90</PRICE>
        <YEAR>1995</YEAR>
    </CD>
    <CD>
        <TITLE>1999 Grammy Nominees</TITLE>
        <ARTIST>Many</ARTIST>
        <COUNTRY>USA</COUNTRY>
        <COMPANY>Grammy</COMPANY>
        <PRICE>10.20</PRICE>
        <YEAR>1999</YEAR>
    </CD>
    <CD>
        <TITLE>For the good times</TITLE>
        <ARTIST>Kenny Rogers</ARTIST>
        <COUNTRY>UK</COUNTRY>
        <COMPANY>Mucik Master</COMPANY>
        <PRICE>8.70</PRICE>
        <YEAR>1995</YEAR>
    </CD>
    <CD>
        <TITLE>Big Willie style</TITLE>
        <ARTIST>Will Smith</ARTIST>
        <COUNTRY>USA</COUNTRY>
        <COMPANY>Columbia</COMPANY>
        <PRICE>9.90</PRICE>
        <YEAR>1997</YEAR>
    </CD>
    <CD>
        <TITLE>Tupelo Honey</TITLE>
        <ARTIST>Van Morrison</ARTIST>
        <COUNTRY>UK</COUNTRY>
        <COMPANY>Polydor</COMPANY>
        <PRICE>8.20</PRICE>
        <YEAR>1971</YEAR>
    </CD>
    <CD>
        <TITLE>Soulsville</TITLE>
        <ARTIST>Jorn Hoel</ARTIST>
        <COUNTRY>Norway</COUNTRY>
        <COMPANY>WEA</COMPANY>
        <PRICE>7.90</PRICE>
        <YEAR>1996</YEAR>
    </CD>
    <CD>
        <TITLE>The very best of</TITLE>
        <ARTIST>Cat Stevens</ARTIST>
        <COUNTRY>UK</COUNTRY>
        <COMPANY>Island</COMPANY>
        <PRICE>8.90</PRICE>
        <YEAR>1990</YEAR>
    </CD>
    <CD>
        <TITLE>Stop</TITLE>
        <ARTIST>Sam Brown</ARTIST>
        <COUNTRY>UK</COUNTRY>
        <COMPANY>A and M</COMPANY>
        <PRICE>8.90</PRICE>
        <YEAR>1988</YEAR>
    </CD>
    <CD>
        <TITLE>Bridge of Spies</TITLE>
        <ARTIST>T'Pau</ARTIST>
        <COUNTRY>UK</COUNTRY>
        <COMPANY>Siren</COMPANY>
        <PRICE>7.90</PRICE>
        <YEAR>1987</YEAR>
    </CD>
    <CD>
        <TITLE>Private Dancer</TITLE>
        <ARTIST>Tina Turner</ARTIST>
        <COUNTRY>UK</COUNTRY>
        <COMPANY>Capitol</COMPANY>
        <PRICE>8.90</PRICE>
        <YEAR>1983</YEAR>
    </CD>
    <CD>
        <TITLE>Midt om natten</TITLE>
        <ARTIST>Kim Larsen</ARTIST>
        <COUNTRY>EU</COUNTRY>
        <COMPANY>Medley</COMPANY>
        <PRICE>7.80</PRICE>
        <YEAR>1983</YEAR>
    </CD>
    <CD>
        <TITLE>Pavarotti Gala Concert</TITLE>
        <ARTIST>Luciano Pavarotti</ARTIST>
        <COUNTRY>UK</COUNTRY>
        <COMPANY>DECCA</COMPANY>
        <PRICE>9.90</PRICE>
        <YEAR>1991</YEAR>
    </CD>
    <CD>
        <TITLE>The dock of the bay</TITLE>
        <ARTIST>Otis Redding</ARTIST>
        <COUNTRY>USA</COUNTRY>
        <COMPANY>Atlantic</COMPANY>
        <PRICE>7.90</PRICE>
        <YEAR>1987</YEAR>
    </CD>
    <CD>
        <TITLE>Picture book</TITLE>
        <ARTIST>Simply Red</ARTIST>
        <COUNTRY>EU</COUNTRY>
        <COMPANY>Elektra</COMPANY>
        <PRICE>7.20</PRICE>
        <YEAR>1985</YEAR>
    </CD>
    <CD>
        <TITLE>Red</TITLE>
        <ARTIST>The Communards</ARTIST>
        <COUNTRY>UK</COUNTRY>
        <COMPANY>London</COMPANY>
        <PRICE>7.80</PRICE>
        <YEAR>1987</YEAR>
    </CD>
    <CD>
        <TITLE>Unchain my heart</TITLE>
        <ARTIST>Joe Cocker</ARTIST>
        <COUNTRY>USA</COUNTRY>
        <COMPANY>EMI</COMPANY>
        <PRICE>8.20</PRICE>
        <YEAR>1987</YEAR>
    </CD>
    <CD>
        <TITLE>Added this record to test program</TITLE>
        <ARTIST>Ray Chappel</ARTIST>
        <COUNTRY>USA</COUNTRY>
        <COMPANY>EMI</COMPANY>
        <PRICE>8.20</PRICE>
        <YEAR>1987</YEAR>
    </CD>
    <CD>
        <TITLE>Another Test Record</TITLE>
        <ARTIST>Bilbo Baggins</ARTIST>
        <COUNTRY>USA</COUNTRY>
        <COMPANY>EMI</COMPANY>
        <PRICE>8.20</PRICE>
        <YEAR>1987</YEAR>
    </CD>
</CATALOG>

what do you mean: I don't know where to go from there?
it says it all in the description.

just check the contents of your nodeslist and use it.

Im not really sure how to do it. Im not sure how to extract the individual records and put them in new files. I've tried to look it up, but havn't really been able to find anything.

this might be a good place to start looking in "how to use xml"

Little old but still usefull free book on XML processing

Thank you to both of you. If anyone else has any suggestions or code that will help, I appreciate it.

You just need to pick up library and get your head around extracting to/from xml. No magic ;)

So this is what I have so far. I am having a problem when I choose "plant_catalog.xml" as the file name. It seems to also trigger the if statement as if I chose "simple.xml". It still runs plant_catalog, but it also runs the simple.xml as well. I had this problem with "cd_catalog.xml" as well, but that problem was fixed by putting the if statement for simple.xml in an else statement. Otherwise, it works fine.

import java.io.*;
import java.io.File;
import java.util.Scanner;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.xml.xpath.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;

public class Chappel_R_a1B 
{//start class

    public static void main(String argv[]) throws Exception 
    {//start main

      //----ask user for name of file they wish to input, used Data Structures and Abstractions with Java texbook by Frank M. Carrano as reference for this----//
            Scanner scanner = new Scanner (System.in);
            System.out.println("Please type in the name of the file you wish to import ('cd_catalog.xml', 'simple.xml', or 'plant_catalog.xml')");
            String fileName = scanner.next();

        //---------iff file chosen is cd_catalog.xml----------------//  
            if (fileName.equals("cd_catalog.xml"))
            {//open if
//removed this piece of code as it is not part of the problem
           }//close if

    //-----------------------------iff fileName is simple.xml-----------------------------------------------------------------------------------------//
          else
          {
          if (fileName.equals("simple.xml"));
          {//start if

                File input = new File("simple.xml");
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                Document doc = dbf.newDocumentBuilder().parse(input);
                XPath xpath = XPathFactory.newInstance().newXPath();

                NodeList nodes = (NodeList) xpath.evaluate("breakfast_menu/food", doc, XPathConstants.NODESET);
                //---------------create new folder--------------------------------//
                File file = new File("Records of simple.xml");
                if (!file.exists()) 
                {//start if
                    if (file.mkdir()) 
                    {//start if
                        System.out.println("Directory is created!");
                    }//end if
                    else 
                    {//start else
                        System.out.println("Failed to create directory!");
                    }//end else
                 }//end if

             int itemsPerFile = 1;
                int fileNumber = 1;
                Document currentDoc = dbf.newDocumentBuilder().newDocument();
                Node rootNode = currentDoc.createElement("breakfast_menu");
                File currentFile = new File("Records of simple.xml/Chappel_R_a1B" +fileNumber+".txt");

                for (int i=1; i <= nodes.getLength(); i++) 
                {//start for
                    Node imported = currentDoc.importNode(nodes.item(i-1), true);
                    rootNode.appendChild(imported);

                    if (i % itemsPerFile == 0) 
                        {//start if

                        writeToFile(rootNode, currentFile);

                        rootNode = currentDoc.createElement("breakfast_menu");
                        currentFile = new File("Records of simple.xml/Chappel_R_a1B"+(++fileNumber)+".txt");
                    }//end if
                 }//end for

                writeToFile(rootNode, currentFile);

                }//close if
                }//close else


          //=-------------------iff fileName is plant_catalog.xml-----------//
          if (fileName.equals("plant_catalog.xml"))
          {//start if
                File input = new File("plant_catalog.xml");
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                Document doc = dbf.newDocumentBuilder().parse(input);
                XPath xpath = XPathFactory.newInstance().newXPath();

                NodeList nodes = (NodeList) xpath.evaluate("CATALOG/PLANT", doc, XPathConstants.NODESET);

                //---------------------create new directory----------------------------------//
                File file = new File("Records of plant_catalog.xml");       
                if (!file.exists()) 
                {//start if
                    if (file.mkdir()) 
                    {//start if
                        System.out.println("Directory is created!");
                    }//end if 
                    else 
                    {//start else
                        System.out.println("Failed to create directory!");
                    }//end else
                }//end if

                int itemsPerFile = 1;
                int fileNumber = 1;
                Document currentDoc = dbf.newDocumentBuilder().newDocument();
                Node rootNode = currentDoc.createElement("CATALOG");
                File currentFile = new File("Records of plant_catalog.xml/Chappel_R_a1B" +fileNumber+".txt");

                for (int i=1; i <= nodes.getLength(); i++) 
                {//start for
                 Node imported = currentDoc.importNode(nodes.item(i-1), true);
                 rootNode.appendChild(imported);

                if (i % itemsPerFile == 0) 
                    {//start if
                    writeToFile(rootNode, currentFile);
                    rootNode = currentDoc.createElement("CATALOG");
                    currentFile = new File("Records of plant_catalog.xml/Chappel_R_a1B"+(++fileNumber)+".txt");
                }//end if
                }//end for

            writeToFile(rootNode, currentFile);
          }//end if
    }//end main



    private static void writeToFile(Node node, File file) throws Exception 
     {//start writeToFile
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.transform(new DOMSource(node), new StreamResult(new FileWriter(file)));
    }//end writeToFile

}//end class

Sorry to early morning for me to think and debug but for start you should use if-else properly

if (fileName.equals("cd_catalog.xml")){

} else if (fileName.equals("simple.xml")){

} else if (fileName.equals("plant_catalog.xml")){

} else {
    System.out.println("File name unknown");
}

What you wrote didnt work, but I got the general idea. It worked when I did

if (fileName.equals("cd_catalog.xml")){
} else if (fileName.equals("simple.xml")){
} else if(fileName.equals("plant_catalog.xml")){
} else {
    System.out.println("File name unknown");
}

So thank you everyone for your help. It has been a long time since I have coded anything and I am new to java.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.