문제

I have a very simple question, yet i cant solve it, I hope you can help me.

How can I read a whole line of an XML file with JDOM? I need the Tag and the attribute and want to save it in one array. How can I do this?

    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;

As you can see, its just the beginning >.<

For the CSV File i did it like that:

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

But for the CSV file is it easy to write it again in the same structure. I saved all variables in different arrays and then i check them.

도움이 되었습니까?

해결책

Is hard to say without knowing the structure of your XML but according to your comments I guess you have something like this:

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

You already have the Document so you need to iterate through the childElement tags. For that:

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

And just iterate through the childElements

다른 팁

If you talk about XML you should not talk about lines, only the start and end tags matter. Lines have no meaning in XML, except for human readability. If you have got the wanted Element-instance you can call getName() and getAttributes() to collect all you information. You could then push them onto any kind of List and covert it into a String[] afterwards.

This does however not make very much sense, because XML in general has a tree structure and you are trying to force it into a flat structure. Moreover, if you want a flat structure look at a Map or a Set, then you can save the key (name of element or attribute) and the value as one pair.

Maybe some XML example showing the gernal schema of your files and the code you use to read the XML so far would be of use.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top