1

我有一个非常简单的问题,但我无法解决,希望你能帮助我。

如何使用 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 文件,很容易以相同的结构再次编写它。我将所有变量保存在不同的数组中,然后检查它们。

4

2 回答 2

1

如果您谈论 XML,您不应该谈论行,只有开始和结束标记很重要。除了人类可读性之外,行在 XML 中没有任何意义。如果您有想要的Element实例,您可以致电getName()getAttributes()收集您的所有信息。然后,您可以将它们推到任何类型上,然后再将List其转换为 a String[]

然而,这并没有多大意义,因为 XML 通常具有树结构,而您正试图将其强制为扁平结构。此外,如果您想查看 aMap或 a的平面结构Set,则可以将键(元素或属性的名称)和值保存为一对。

也许一些 XML 示例显示您的文件的 gernal 模式以及您迄今为止用于读取 XML 的代码将是有用的。

于 2013-04-17T09:05:34.923 回答
1

在不知道 XML 的结构的情况下很难说,但根据您的评论,我猜您有这样的事情:

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

您已经拥有 Document,因此您需要遍历 childElement 标记。为了那个原因:

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

只需遍历 childElements

于 2013-04-18T19:00:00.230 回答