XML write Problem

MarioK

Aktives Mitglied
Hallo Gemeinschaft,
ich habe mit den unten dargestellten Code folgendes XML dokument erzeugt: (ich sage gleich dazu, dass ich mich das erste Mal mit XML beschäftige, darum bitte ich auch um erklärende Worte)
[XML]<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Knoten>
<koordinaten>
<id>0</id>
<xkoord>111</xkoord>
<ykoord>222</ykoord>
</koordinaten>
<koordinaten>
<id>1</id>
<xkoord>333</xkoord>
<ykoord>444</ykoord>
</koordinaten>
</Knoten>[/XML]

ich möchte aber folgende Ausgabe erreichen, komme aber derzeit nicht auf den Punkt, quasi ich weiss nicht wie bzw wie bekomme ich die ID so da rein:
[XML]<?xml version="1.0" encoding="UTF-8"?>
<Knoten>
<koordinaten id = "0">
<xkoord>111</xkoord>
<ykoord>222</ykoord>
</koordinaten>
<koordinaten id = "1">
<xkoord>333</xkoord>
<ykoord>444</ykoord>
</koordinaten>
</Knoten>[/XML]

mit folgenden Code habe ich das obere .xml Dokument erzeugt:
Java:
public class Punkt {

	private int xkoord;
	private int ykoord;
	private int id;
	
	public Punkt() {}
	
	public Punkt(int id, int xkoord, int ykoord) {
		this.id = id;
		this.xkoord = xkoord;
		this.ykoord = ykoord;
	}
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
	
	public int getXkoord() {
		return xkoord;
	}

	public void setXkoord(int xkoord) {
		this.xkoord = xkoord;
	}
	
	public int getYkoord() {
		return ykoord;
	}

	public void setYkoord(int ykoord) {
		this.ykoord = ykoord;
	}
	
	@Override
	public String toString() {
		return this.id + "(" + this.xkoord + " " + this.ykoord + ")";
	}

}
+
Java:
import java.util.*;

import javax.xml.bind.annotation.*;


@XmlRootElement(name ="Knoten") 
public class PunkteToXML {
	private ArrayList<Punkt> kreise;

	public PunkteToXML() {}
	
	public PunkteToXML(ArrayList<Punkt> kreise) {
		this.kreise = kreise;
	}
	
	@XmlElement(name = "koordinaten")
	public ArrayList<Punkt> getKreise() {
		return kreise;
	}

	public void setListe(ArrayList<Punkt> kreise) {
		this.kreise = kreise;
	}
}
und
Java:
import javax.xml.bind.*;

import java.io.*;
import java.util.*;

public class PunkteXMLWrite {
		/**
		 * Methode zum Schreiben von Punkten in Datei im XML-Format
		 * @param punkteToXML die zu schreibenden Punkte
		 * @param file die Datei in die die Punkte im XML-Format geschrieben werden sollen
		 * @throws JAXBException
		 */
		public void writePunkteToXML(PunkteToXML punkteToXML, File file) throws JAXBException {
			JAXBContext jc = JAXBContext.newInstance(PunkteToXML.class);
			Marshaller m = jc.createMarshaller();
			m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
			m.marshal(punkteToXML, file);
		}

		/**
		 * @param args
		 */
		public static void main(String[] args) {
			// TODO Auto-generated method stub
			PunkteXMLWrite meineDB = new PunkteXMLWrite();
			
			ArrayList<Punkt> kreise = new ArrayList<Punkt> ();
			kreise.add(new Punkt(0, 111, 222));			
			kreise.add(new Punkt(1, 333, 444));
			System.out.println(kreise);
			System.out.println();
			PunkteToXML meinePunkte = new PunkteToXML(kreise);
			
			/* Ausgabe der Liste mit Punkten */
			for (Punkt pkt: meinePunkte.getKreise()) {
				System.out.println(pkt);
			}
			
			// Generierung des XML-Dokumentes mit Punkten
			File f = new File("data/meinePunkte.xml");
			try {
				meineDB.writePunkteToXML(meinePunkte, f);
			}
			catch (Exception e) {
				System.out.println(e.getMessage());
				System.out.println("Fehler Auslesen");
			}

		}
}

vielleicht kann der ein oder andere ein Gedankenanstoss geben....
 
Zuletzt bearbeitet:
Füge mal @XmlAttribute dem
Code:
getId
in deiner Punkt Klasse hinzu:
Java:
public class Punkt {
	 
    private int xkoord;
    private int ykoord;
    private int id;
    
    public Punkt(){}
    
    public Punkt(int id, int xkoord, int ykoord) {
        this.id = id;
        this.xkoord = xkoord;
        this.ykoord = ykoord;
    }

    @XmlAttribute
    public int getId() {
        return this.id;
    }
 
    public void setId(int id) {
        this.id = id;
    }
    
    public int getXkoord() {
        return xkoord;
    }
 
    public void setXkoord(int xkoord) {
        this.xkoord = xkoord;
    }
    
    public int getYkoord() {
        return ykoord;
    }
 
    public void setYkoord(int ykoord) {
        this.ykoord = ykoord;
    }
    
    @Override
    public String toString() {
        return this.id + "(" + this.xkoord + " " + this.ykoord + ")";
    }
 
}
 

Zurück
Oben