PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : XML SaxReader ....



nul
14-04-2006, 13:06
Ich brauch wieder mal eure Hilfe zum Thema XML ...
und zwar hab ich folgendes File:

<?xml version="1.0"?>
<Info>

<Semaphore>
<name>Semaphore</name>
<list type="simple">
<item>Sem</item>
<item>PID</item>
</list>

<list type="advanced">
<item>Sem</item>
<item>PIDadvanced</item>
<item>...</item>
</list>
</Semaphore>

<Mailbox>
<name>Mailbox</name>
<list type="simple">
<item>Mbx</item>
<item>PID</item>
</list>

<list type="advanced">
<item>Mbx</item>
<item>PID</item>
<item>...</item>
</list>
</Mailbox>

</Info>

Dazu hab ich zwei Klassen, Semaphore und Mailbox die jeweils als Attribute den namen und zwei listen enthalten.

Nun moechte ich mir einen XMLReader schreiben dem man entweder Mailbox oder Semaphore uebergeben kann und er mir dann die zwei listen und den namen fuellt.

Im Moment sieht das ganze so aus:

package core.parser;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;


public class SaxReader extends DefaultHandler {

public static void main(String args[] ) throws Exception {
File file = new File( "file.xml" );

SaxReader reader = new SaxReader( new InputSource( new FileInputStream( file ) ) );
reader.parse();
}

private InputSource source;
private XMLReader reader;

public SaxReader( InputSource source ) throws SAXException {
super();
this.source = source;
this.reader = XMLReaderFactory.createXMLReader();
}

private void parse() throws IOException, SAXException {
this.reader.setDTDHandler( this );
this.reader.setContentHandler( this );
this.reader.setErrorHandler( this );
this.reader.parse( this.source );
}

public void startDocument() {
System.out.println( "Start parsing ..." );
}

public void endDocument() {
System.out.println( "End parsing ..." );
}

public void startElement( String uri, String localName, String qName, Attributes atts ) {
System.out.println( "Start element: " + qName );

for ( int i = 0; i < atts.getLength(); i++ ) {
System.out.println( "Attr: " + atts.getLocalName( i ) + " " + atts.getValue( i ) );
}
}

public void endElement( String uri, String localName, String qName ) {
System.out.println( "End element: " + qName );
}

public void characters(char[] ch, int start, int length) {
StringBuffer str = new StringBuffer( );
str.append( ch );

String s = str.substring( start, start + length );
if ( s.trim().length() > 0 ) {
System.out.println( start + " " + length + " " + str.substring( start, start + length ) );
}
}
}

Damit gebe ich nun die Daten aus, aber wie kann ich jetzt die zwei listen und den Namen fuellen?

mfg cg

nul
14-04-2006, 13:56
Habs jetzt geschafft.
Hab die XML-Datei ein wenig umgestellt:

<?xml version="1.0"?>
<SInfo>

<info type="Semaphore">
<name>Semaphore</name>
<list type="simple">
<item>Sem</item>
<item>PID</item>
</list>

<list type="advanced">
<item>Sem</item>
<item>PIDadvanced</item>
<item>...</item>
</list>
</info>

<info type="Mailbox">
<name>Mailbox</name>
<list type="simple">
<item>Mbx</item>
<item>PID</item>
</list>

<list type="advanced">
<item>Mbx</item>
<item>PID</item>
<item>...</item>
</list>
</info>

</SInfo>


package core.parser;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.LinkedList;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;


public class SaxReader extends DefaultHandler {

public static void main(String args[] ) throws Exception {
File file = new File( "file.xml" );

SaxReader reader = new SaxReader( new InputSource( new FileInputStream( file ) ) );
reader.setNamespace( "Semaphore" );
reader.parse();
}

private InputSource source;
private XMLReader reader;
private String namespace;
private String cNamespace;

private enum XmlType { NAME, LIST_SIMPLE, LIST_ADVANCED };
private XmlType type;

private String name;
private LinkedList<String> simpleList;
private LinkedList<String> advanceList;

public SaxReader( InputSource source ) throws SAXException {
super();
this.source = source;
this.reader = XMLReaderFactory.createXMLReader();
this.namespace = null;
this.cNamespace = null;

this.name = "";
this.simpleList = new LinkedList<String>();
this.advanceList = new LinkedList<String>();
}

private void parse() throws IOException, SAXException {
this.reader.setContentHandler( this );
this.reader.parse( this.source );
}

public void setNamespace( String namespace ) {
this.namespace = namespace;
}

public void startDocument() {
System.out.println( "Start parsing ..." );
}

public void endDocument() {
System.out.println( "End parsing ..." );

System.out.println( "Name: " + this.name );
System.out.println( "simpleList: ");
for ( int i = 0; i < this.simpleList.size(); i++ )
System.out.println( this.simpleList.get( i ) );

System.out.println( "\nadvancedList: ");
for ( int i = 0; i < this.advanceList.size(); i++ )
System.out.println( this.advanceList.get( i ) );
}

public void startElement( String uri, String localName, String qName, Attributes atts ) {
if ( qName.equals( "info" ) ) {
for ( int i = 0; i < atts.getLength(); i++ ) {
if ( atts.getLocalName( i ).equals( "type" ) ) {
this.cNamespace = atts.getValue( i );
}
}
} else if ( qName.equals( "name" ) ) {
this.type = XmlType.NAME;
} else if ( qName.equals( "list" ) ) {
for ( int i = 0; i < atts.getLength(); i++ ) {
if ( atts.getLocalName( i ).equals( "type" ) ) {
if ( atts.getValue( i ).equals( "simple") ) {
this.type = XmlType.LIST_SIMPLE;
} else if ( atts.getValue( i ).equals( "advanced") ) {
this.type = XmlType.LIST_ADVANCED;
}
}
}
}
}

public void endElement( String uri, String localName, String qName ) {
}

public void characters(char[] ch, int start, int length) {
StringBuffer buffer = new StringBuffer();
buffer.append( ch );

String str = buffer.substring( start, start+length );
if ( str.trim().length() > 0 ) {
this.add( str );
}
}

private void add( String s ) {

if ( this.cNamespace.equals( this.namespace ) ) {
switch ( this.type ) {
case LIST_ADVANCED:
this.advanceList.add( s );
break;
case LIST_SIMPLE:
this.simpleList.add( s );
break;
case NAME:
this.name = s;
break;
default:
break;
}
}
}
}


Ich finde meine Loesung aber nicht gerade schoen, hat vielleicht jemand verbesserungsvorschlaege?