Parsing XML file using a for loop

Posted by Johnny Spintel on Stack Overflow See other posts from Stack Overflow or by Johnny Spintel
Published on 2014-06-06T15:22:34Z Indexed on 2014/06/06 15:24 UTC
Read the original article Hit count: 207

Filed under:
|
|
|
|

I have been working on this program which inserts an XML file into a MYSQL database. I'm new to the whole .jar idea by inserting packages. Im having an issue with parse(), select(), and children(). Can someone inform me how I could fix this issue? Here is my stack trace and my program below:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    The method select(String) is undefined for the type Document
    The method children() is undefined for the type Element
    The method children() is undefined for the type Element
    The method children() is undefined for the type Element
    The method children() is undefined for the type Element

at jdbc.parseXML.main(parseXML.java:28)

import java.io.*;
import java.sql.*;

import org.jsoup.Jsoup;
import org.w3c.dom.*;

import javax.xml.parsers.*;

public class parseXML{
    public static void main(String xml) {

        try{
        BufferedReader br = new BufferedReader(new FileReader(new File("C:\\staff.xml")));
        String line;
        StringBuilder sb = new StringBuilder();

        while((line=br.readLine())!= null){
               sb.append(line.trim());
            }   

        Document doc = Jsoup.parse(line);

        StringBuilder queryBuilder;
        StringBuilder columnNames;
        StringBuilder values;

        for (Element row : doc.select("row")) {   
            // Start the query   
            queryBuilder = new StringBuilder("insert into customer(");
            columnNames = new StringBuilder();
            values = new StringBuilder();

            for (int x = 0; x < row.children().size(); x++) {

                // Append the column name and it's value 
                columnNames.append(row.children().get(x).tagName());
                values.append(row.children().get(x).text());

                if (x != row.children().size() - 1) {
                    // If this is not the last item, append a comma
                    columnNames.append(",");
                    values.append(",");
                }
                else {
                    // Otherwise, add the closing paranthesis
                    columnNames.append(")");
                    values.append(")");
                }                                
            }

            // Add the column names and values to the query
            queryBuilder.append(columnNames);
            queryBuilder.append(" values(");
            queryBuilder.append(values);

            // Print the query
            System.out.println(queryBuilder);
        }

        }catch (Exception err) {
        System.out.println(" " + err.getMessage ());
        }
    }
}

© Stack Overflow or respective owner

Related posts about java

Related posts about mysql