2012-07-04 44 views
1

我对Java和Xml解析非常新颖。我的要求是获取一个XML文件,并使用java以表格和列格式将XML文件的数据存储在数据库中。我在谷歌尝试为它获得正确的解决方案。但我很无奈。直到现在我所做的是,我可以动态获取XML数据并存储标记名称或值。但我的要求是只将标签名称作为列名称和与行格式中的特定列相关的数据,任何人都可以更正我的代码。使用Java检索XMl数据

<?xml version="1.0"?> 
<company> 
    <staff> 
     <firstname>yong</firstname> 
     <lastname>mook kim</lastname> 
     <nickname>mkyong</nickname> 
     <salary>100000</salary> 
    </staff> 
    <staff> 
     <firstname>low</firstname> 
     <lastname>yin fong</lastname> 
     <nickname>fong fong</nickname> 
     <salary>200000</salary> 
    </staff> 
</company> 

Java代码的

import java.io.*; 
    import javax.xml.parsers.*; 
    import org.w3c.dom.*; 
    import org.xml.sax.*; 

    public class XmlData{ 
    static public void main(String[] arg){ 
    try { 
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); 
    System.out.print("Enter XML File Path: "); 
    String xmlFile = bf.readLine(); 
    //Store the String into the File 
    File file = new File(xmlFile); 
    if(file.exists()){ 
    // Create a factory 
    DocumentBuilderFactory factory =DocumentBuilderFactory.newInstance(); 
    // Use the factory to create a builder 
    DocumentBuilder builder = factory.newDocumentBuilder(); 
    Document doc = builder.parse(xmlFile); 
    Element docEle = doc.getDocumentElement(); 
    System.out.println("Root element of the document: "+ docEle.getNodeName()); 
    // Get a list of all elements in the document 
    NodeList list = doc.getElementsByTagName("*"); 

    int totalElements = list.getLength(); 

    System.out.println("XML Elements: " + totalElements); 
    for (int i=0; i<list.getLength(); i++) 
    { 
    // Get element 
    Element element = (Element)list.item(i); 
    String tag=element.getTagName(); 
    String name=list.item(0).getChildNodes().item(0).getNodeValue(); 
    System.out.println(name); 
    System.out.print(tag); 
    System.out.print(" "); 
    //System.out.println(element.getNodeName()); 


    } 
    } 
    else{ 
    System.out.print("File not found!"); 
    } 
    } 
    catch (Exception e) { 
    System.exit(1); 
    } 
    } 
    } 

输出此代码:

company 
staff 
firstname 
lastname 
nickname 
salary 
staff 
firstname 
lastname 
nickname 
salary 

预期输出:

FirstName, Lastname, nickname , salary //column 

yong  ,mook kim, mkyong  , 100000 // rows 
low  ,yin fong, fong fong ,200000 

回答

3

我假设你已经创建了一个数据库表四列 - FirstNAme,LastName, NickName,薪水。

读取数据并存储在数据库中非常简单。

import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import org.w3c.dom.Document; 
import org.w3c.dom.Element; 
import org.w3c.dom.NodeList; 

public class ParseStaff { 
    public static void main(String [] args){ 
     parseFile(); 
    } 
    public static void parseFile() { 
     //get the factory 
     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
     try { 
      //Using factory get an instance of document builder 
      DocumentBuilder db = dbf.newDocumentBuilder(); 
      //parse using builder to get DOM representation of the XML file 
      //Document dom = db.parse("employees.xml"); 
      Document dom = db.parse("C:\\GAE\\NetBeansProjects\\Test\\src\\statff.xml"); 
      //get the root element 
      Element docEle = dom.getDocumentElement(); 
      //get a nodelist of elements 
      NodeList nl = docEle.getElementsByTagName("staff"); 

      if (nl != null && nl.getLength() > 0) { 
       for (int i = 0; i < nl.getLength(); i++) { 
        //get the employee element 
        Element el = (Element) nl.item(i); 
        String firstname = getTextValue(el, "firstname"); 
        String lastname = getTextValue(el, "lastname"); 
        String nickname = getTextValue(el, "nickname"); 
        int salary = getIntValue(el, "salary"); 

        System.out.println(firstname); 
        System.out.println(lastname); 
        System.out.println(nickname); 
        System.out.println(salary); 
       } 
      } 

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

    } 
    private static String getTextValue(Element ele, String tagName) { 
     String textVal = null; 
     NodeList nl = ele.getElementsByTagName(tagName); 
     if (nl != null && nl.getLength() > 0) { 
      Element el = (Element) nl.item(0); 
      textVal = el.getFirstChild().getNodeValue(); 
     } 

     return textVal; 
    } 
    private static int getIntValue(Element ele, String tagName) { 
     return Integer.parseInt(getTextValue(ele, tagName)); 
    } 
} 

会打印:

yong 
mook kim 
mkyong 
100000 
low 
yin fong 
fong fong 
200000 
+0

+1 @sreehari:的确是getTextValue helper方法是至关重要的!但我不应该说这是非常简单的,因为需要实现这个非常普遍的需求的复杂性XD – helios

+0

嗨,这不是我想要的,通过动态我需要创建列名称,而不是一个接一个我想在列和行格式 – Navyah