2011-07-25 12 views
0

次のコードを使用してXMLファイルを読み取ることができます。arraylistを使用してxmlをデバイスデータベースに追加する方法を知る必要があります。javaのarraylistを使用してデータベースにxmlファイルを追加する

XMLコード:

public static void main(String argv[]) { 

    try { 

File fXmlFile = new File("c:\\testing.xml"); 
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
Document doc = dBuilder.parse(fXmlFile); 
doc.getDocumentElement().normalize(); 


NodeList nList = doc.getElementsByTagName("device"); 

for (int temp = 0; temp < nList.getLength(); temp++) { 

    Node nNode = nList.item(temp);  
    if (nNode.getNodeType() == Node.ELEMENT_NODE) { 

    Element eElement = (Element) nNode; 
    ArrayList<Device> arrayList = new ArrayList<Device>(); 

    //System.out.println(arrayList); 
    String type=getTagValue("type", eElement); 
    String Name=getTagValue("name", eElement); 
    String Setup=getTagValue("setup", eElement); 
    arrayList.add(type); 
    arrayList.add(Name); 
    arrayList.add(Setup); 
    System.out.println(arrayList); 



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

    private static String getTagValue(String sTag, Element eElement){ 
    NodeList nlList= eElement.getElementsByTagName(sTag).item(0).getChildNodes(); 
    Node nValue = (Node) nlList.item(0); 

    return nValue.getNodeValue();  
} 

    } 

Device.java

import java.util.HashSet; 
    import java.util.Set; 

    import javax.persistence.CascadeType; 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
    import javax.persistence.Id; 
    import javax.persistence.JoinColumn; 
import javax.persistence.JoinTable; 
import javax.persistence.OneToMany; 
    import javax.persistence.Table; 
@Entity 
@Table(name = "DEVICE") 
public class Device { 
    private String type; 
    private String setup; 
    private String name; 
    private Long deviceId; 
    private Set<CommandInfo> commandSet = new HashSet<CommandInfo>(); 

    public Device() { 

    } 

    public Device(String name, String type, String setup) { 
      System.out.println("name = "+name+" type = "+type+" setup = "+setup); 
      this.name = name; 
      this.type = type; 
      this.setup = setup; 
    } 

    /** 
    * @return the type 
    */ 
    public String getType() { 
     return type; 
    } 

    /** 
    * @param type the type to set 
    */ 
    public void setType(String type) { 
     this.type = type; 
    } 

    /** 
    * @return the setup 
    */ 
    public String getSetup() { 
     return setup; 
    } 

    /** 
    * @param setup the setup to set 
    */ 
    public void setSetup(String setup) { 
     this.setup = setup; 
    } 

    /** 
    * @return the name 
    */ 
    public String getName() { 
     return name; 
    } 

    /** 
    * @param name the name to set 
    */ 
    public void setName(String name) { 
     this.name = name; 
    } 

    /** 
    * @return the id 
    */ 
    @Id 
    @GeneratedValue 
    @Column(name = "DEVICE_ID") 
    public Long getDeviceId() { 
     return deviceId; 
    } 

    /** 
    * @param id the id to set 
    */ 
    public void setDeviceId(Long id) { 
     this.deviceId = id; 
    } 

    /** 
    * @return the commandSet 
    */ 
    @OneToMany(cascade = CascadeType.ALL) 
    @JoinTable(name = "DEVICE_COMMAND", joinColumns = { 
      @JoinColumn(name = "DEVICE_ID") }, 
      inverseJoinColumns = { @JoinColumn(name = "COMMAND_ID") 
    }) 
    public Set<CommandInfo> getCommandSet() { 
     return commandSet; 
    } 

    /** 
    * @param commandSet the commandSet to set 
    */ 
    public void setCommandSet(Set<CommandInfo> commandSet) { 
     this.commandSet = commandSet; 
    } 

    /* (non-Javadoc) 
    * @see java.lang.Object#toString() 
    */ 
    @Override 
    public String toString() { 
     StringBuilder builder = new StringBuilder(); 
     builder.append("Device [type="); 
     builder.append(type); 
     builder.append(", setup="); 
     builder.append(setup); 
     builder.append(", name="); 
     builder.append(name); 
     builder.append(", deviceId="); 
     builder.append(deviceId); 
     builder.append(", commandSet="); 
     builder.append(commandSet); 
     builder.append("]"); 
     return builder.toString(); 
    } 


    } 

誰もが事前にJavaの.ThanksでデータベースにこのXMLオブジェクトを追加する方法を手伝ってくれる。

+0

ご意見:あなたの質問を変更し、本当に必要なものを記述する必要があります。 – home

答えて

1

生のXMLをデータベースに保存する場合は、Deviceクラスのbyte[]属性を使用できます。次に、XML文書をバイト配列に変換し、それに応じて格納します。大きなファイルを扱わなければならない場合は、ストリーミングも見てください。

+0

または、すでに抽出された属性のみを永続化しますか?その場合は、EntityManagerを取得し、Deviceのインスタンスを作成して永続化する必要があります。 – home

+0

いいえいいえ、私はデバイスクラスにrawファイルを追加したくありません。各ノードの内容をデバイスクラスに追加します。 – bharathi

+0

ええ、EntityManagerを入手する必要があるので、たくさんのチュートリアルが存在します:http://download.oracle.com/docs/cd/B31017_01/web.1013/b28221/usclient005.htm#CIHIAHIE – home

関連する問題