2013-04-17 78 views
1

我有一個非常簡單的問題,但我無法解決它,我希望你能幫助我。閱讀整個XML行並將其保存到數組中? JDOM

我怎樣才能讀取JDOM的XML文件的一整行?我需要標籤和屬性,並希望將其保存在一個數組中。我怎樣才能做到這一點?

package converter; 

import java.io.FileReader; 
import java.io.FileWriter; 
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.List; 

import javax.swing.JOptionPane; 

import au.com.bytecode.opencsv.CSVReader; 
import au.com.bytecode.opencsv.CSVWriter; 

import org.jdom2.Document; 
import org.jdom2.input.*; 
import org.jdom2.output.*; 

public class Converter { 

    public List<Entry> xmlconvert(String pfad, String pfad2, String bitmask){ 
     List<Entry> entry = new ArrayList<Entry>(); 
     List<Entry> wrongEntries = new ArrayList<Entry>(); 
     String wrongEntryIndexes = ""; 

     String[] languages = {"en", "pt", "it", "fr", "es", "de", "zh"}; 

     try{ 


     SAXBuilder builder = new SAXBuilder(); 
     Document doc = builder.build(pfad); 

     JOptionPane.showMessageDialog(null, "Converting successful."); 
     return entry; 

正如你所看到的,它剛剛開始>。 <

CSV文件我做到了這樣:

public List<Entry> convert(String pfad, String pfad2, String bitmask) { 

    List<Entry> entry = new ArrayList<Entry>(); 
    List<Entry> wrongEntries = new ArrayList<Entry>(); 
    String wrongEntryIndexes = ""; 

    String[] languages = {"en", "pt", "it", "fr", "es", "de", "zh"}; 

    try { 

     CSVReader reader = new CSVReader(new FileReader(pfad), ';', '\"', 1); 

     String [] nextLine; 

     while ((nextLine = reader.readNext()) != null) { 
      Entry entryi = new Entry(); 
      entryi = new Entry(); 
      entryi.termEntryID = nextLine[0]; 
      entryi.termEntryUUID = nextLine[1]; 
      entryi.termID = nextLine[2]; 
      entryi.termUUID = nextLine[3]; 
      entryi.term = nextLine[4]; 
      entryi.status = nextLine[5]; 
      entryi.language = nextLine[6]; 
      entryi.domains = nextLine[7]; 
      entryi.morphosyntacticRestriction = nextLine[8]; 
      entryi.variantsConfiguration = nextLine[9]; 
      entryi.isHeadTerm = nextLine[10]; 
      entryi.checkInflections = nextLine[11]; 
      entryi.frequency = nextLine[12]; 
      entryi.createdBy = nextLine[13]; 
      entryi.createdOn = nextLine[14]; 
      entryi.changedBy = nextLine[15]; 
      entryi.changedOn = nextLine[16]; 
      entryi.context = nextLine[17]; 
      entryi.crossReference = nextLine[18]; 
      entryi.definitionDE = nextLine[19]; 
      entryi.definitionEN = nextLine[20]; 
      entryi.example = nextLine[21]; 
      entryi.externalCrossReference = nextLine[22]; 
      entryi.gender = nextLine[23]; 
      entryi.geographicalUsage = nextLine[24]; 
      entryi.imageURL = nextLine[25]; 
      entryi.note = nextLine[26]; 
      entryi.numerus = nextLine[27]; 
      entryi.partOfSpeech = nextLine[28]; 
      entryi.processStatus = nextLine[29]; 
      entryi.sourceOfDefinition = nextLine[30]; 
      entryi.sourceOfTerm = nextLine[31]; 
      entryi.termType = nextLine[32]; 
      entry.add(entryi); 
     } 

但CSV文件是很容易在同一結構再寫一遍。我將所有變量保存在不同的數組中,然後檢查它們。

+0

如果你已經嘗試任何事情,請告訴我們。 – Suranga

+0

@Jagson,DIY。 _PS_:爲什麼要將標記和屬性存儲在一個數組中?那麼屬性和文本/節點值的值呢? – hiway

+0

實際上不是,我試圖爲XML文件構建一個轉換器。轉換器應搜索一些參數,刪除它們,然後將其與其他7個不同的參數相乘。現在我正在考慮如何才能達到最佳效果。我其實有CSV文件的程序。但現在我真的很困惑,我可以如何做到與XML文件相同。 – Jagson

回答

1

是很難不知道你的XML的結構,但根據你的意見,說我想你有這樣的事情:

<parentElement> 
    <childElement> 
     <attr1>XXX</attr1> 
     .... 
    </childElement> 
    ... more childElements 
</parentElement> 

你已經有文件,所以你需要通過元素childElement標籤進行迭代。對於:

Element root = doc.getRootElement(); 
List<Element> childElements = root.getChildren("childElement"); 

而剛剛遍歷的childElements

0

如果你談論XML,你不應該談論線路,只有開始和結束標籤很重要。除了人的可讀性外,行在XML中沒有意義。如果你有想要的Element -instance,你可以撥打getName()getAttributes()來收集你所有的信息。然後,您可以將它們推入任何種類的List,然後將其轉換爲String[]

然而,這並沒有多大意義,因爲XML通常具有樹結構,並且您試圖將其強制爲扁平結構。此外,如果你想要一個平面結構看看MapSet,那麼你可以將鍵(元素或屬性的名稱)和值保存爲一對。

也許一些XML示例顯示了您的文件的gernal模式,以及您用來閱讀XML的代碼,這些都是有用的。

+0

啊,聽起來不錯!但是,我怎麼才能真正重寫保存的信息與原始文件相同的結構呢? – Jagson

+0

你可以編輯你的主要問題。這會讓人們更容易幫助你:-) –

+0

完成,對不起; – Jagson

相關問題