2014-06-06 4 views
0

나는이 프로그램을 사용하여 XML 파일을 MYSQL 데이터베이스에 삽입했다. 나는 패키지를 삽입함으로써 전체 .jar 아이디어에 새로운 것이다. parse(), select() 및 children()에 문제가 있습니다. 누군가이 문제를 해결할 수있는 방법을 알려 줄 수 있습니까? 당신이 요소를 사용할 때, 컴파일러는 org.w3c.dom의 요소 인터페이스에서 찾고있다 -for 루프를 사용하여 XML 파일 구문 분석

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()); 
     } 
    } 
} 

답변

1

는 두 개의 별도의 라이브러리 이름은 충돌 : 여기 내 스택 추적 및 아래에있는 내 프로그램입니다 . *; . 당신이 정말로

Jsoup

에서 요소를 사용하여 라인 제거하려는 반면에 - 수입 org.w3c.dom의를 *

라인 수입 org.jsoup.Jsoup를 추가합니다. *; (참고 *)

+2

제쳐두고, ** 두 버전을 모두 사용해야하는 경우, 정상적인 방법으로 하나를 가져 와서 * 다른 버전에 대한 참조를 * 완전하게 한정 할 수 있습니다 (그래서'import org .jsoup.Jsoup.Element' 그리고 다른 종류가 필요할 때마다'Element'가 아닌'org.w3c.dom.Element'로 한정하십시오). – JonK