Hallo, ich bin dabei ein Programm zu schreiben das eine .xml Datei ausliest und den Inhalt dann wiedergibt.
Leider wird nur dies angezeigt:
Hier ist die .xml Datei:
Hier ist mein Code:
Ich hoffe ihr könnt mir helfen.
JavaGamer
Leider wird nur dies angezeigt:
Eigentlich soll aber dies angezeigt werden:Dieser Brief ist von wohnhaft in der text, text vom date, date
Dieser Brief ist von title, title wohnhaft in der text, text vom date, date
Hier ist die .xml Datei:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<news>
<title>
title, title
</title>
<text>
text, text
</text>
<date>
date, date
</date>
</news>
Hier ist mein Code:
Java:
package volcanoplayer.launcher.news;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
public class SAX
{
// sets the file name, that the program know's the name of the file
private String filename = "." + File.separator + "news.xml";
public SAX()
{
this.getNews();
}
public void getNews()
{
try
{
// creates a new XML file reader
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
// creates a new file reader
FileReader reader = new FileReader(filename);
// Gibt die Eingabequelle an
InputSource inputSource = new InputSource(reader);
// sets the content handler (shows what is in the file and what must do)
xmlReader.setContentHandler(new NewsHandler());
// Parsen wird gestartet
xmlReader.parse(inputSource);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
catch (SAXException e)
{
e.printStackTrace();
}
}
}
Java:
package volcanoplayer.launcher.news;
public class News2
{
private int id;
private String title;
private String text;
private String date;
/**
* @return the id
*/
public int getID()
{
return id;
}
public void setID(int id)
{
this.id = id;
}
/**
* @return the title
*/
public String getTitle()
{
return title;
}
/**
* @return the text
*/
public String getText()
{
return text;
}
/**
* @return the date
*/
public String getDate()
{
return date;
}
/**
* @param title the title to set
*/
public void setTitle(String title)
{
this.title = title;
}
/**
* @param text the text to set
*/
public void setText(String text)
{
this.text = text;
}
/**
* @param date the date to set
*/
public void setDate(String date)
{
this.date = date;
}
/**
* @param title
* @param text
* @param date
*/
public News2(String title, String text, String date)
{
super();
this.title = title;
this.text = text;
this.date = date;
}
@Override
public String toString()
{
return "Dieser Brief ist von "+ this.title +" wohnhaft in der " + this.text.trim() + " vom " + this.date.trim();
}
}
Java:
package volcanoplayer.launcher.news;
import java.util.ArrayList;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
public class NewsHandler implements ContentHandler
{
private ArrayList<News2> alleLetter = new ArrayList<News2>();
private String currentValue;
private News2 news;
// Aktuelle Zeichen die gelesen werden, werden in eine Zwischenvariable
// gespeichert
public void characters(char[] ch, int start, int length) throws SAXException
{
currentValue = new String(ch, start, length);
}
// Methode wird aufgerufen wenn der Parser zu einem Start-Tag kommt
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException
{
if (localName.equals("news"))
{
// Neue News erzeugen
news = new News2(null,null,null);
}
// Attribut id wird in einen Integer umgewandelt und dann zu der
// jeweiligen Person gesetzt
//news.setId(Integer.parseInt(atts.getValue("id")));
}
// Methode wird aufgerufen wenn der Parser zu einem End-Tag kommt
public void endElement(String uri, String localName, String qName) throws SAXException
{
// Titel setzen
if (localName.equals("title"))
{
news.setTitle(currentValue);
}
// Text setzen
if (localName.equals("text"))
{
news.setText(currentValue);
}
// Datum setzen
if (localName.equals("date"))
{
news.setDate(currentValue);
}
// Person in Personenliste abspeichern falls Person End-Tag erreicht
// wurde.
if (localName.equals("news"))
{
alleLetter.add(news);
System.out.println(news);
}
}
public void endDocument() throws SAXException {}
public void endPrefixMapping(String prefix) throws SAXException {}
public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {}
public void processingInstruction(String target, String data) throws SAXException {}
public void setDocumentLocator(Locator locator) { }
public void skippedEntity(String name) throws SAXException {}
public void startDocument() throws SAXException {}
public void startPrefixMapping(String prefix, String uri) throws SAXException {}
}
Ich hoffe ihr könnt mir helfen.
JavaGamer