Hilfe beim bearbeiten von XML elemente

Faiza

Mitglied
Hallo zusammen,

ich sitze hier seit vier Stunden und suche meine Fehler, aber ich sehe ihn echt nicht.:rtfm:
ich möchte meine Attribute basetype, type, position und mode auslesen, schaffe ich aber nicht. Die restliche habe ich schon. Könntet ihr BITTE BITTE drüber gucken???

Vielen Dank für eure helfe im voraus!!!

Java:
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class OptWindow {

	ArrayList<VariableHandler> component;
	Document dom;

	public OptWindow() {
		// create a list to hold the Element objects
		component = new ArrayList<VariableHandler>();
	}

	public void run() {
		// parse the xml file and get the dom object
		parseXmlFile();

		// get each component element and create a component object
		parseDocument();

		// Iterate through the list and print the data
		printData();
	}

	private void parseXmlFile() {
		// get the factory
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		try {

			// Using factory get an instance of document builder
			DocumentBuilder db = dbf.newDocumentBuilder();
			// parse using builder to get DOM representation of the XML file
			dom = db.parse("OptimISEGuiModel.xml");
			
		} catch (ParserConfigurationException pce) {
			pce.printStackTrace();
		} catch (SAXException se) {
			se.printStackTrace();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
	}

	private void parseDocument() {
		//get the root elememt
		Element docEle = dom.getDocumentElement();
		
		//get a nodelist of <component> elements
		NodeList nl = docEle.getElementsByTagName("MainButton");
		if(nl != null && nl.getLength() > 0) {
			for(int i = 0 ; i < nl.getLength();i++) {
				
				//get the component element
				Element el = (Element)nl.item(i);
				
				//get the component object
				VariableHandler var = getOptComponent(el);
				
				//add it to list
				component.add(var);
			}
		}
	}

	private VariableHandler getOptComponent(Element cp) {
		//for each <component> element get text or int or char values of 
		String ChartName = getTextValue(cp,"ChartName");
		String basetype = cp.getAttribute("basetype");
		boolean useAlertColors = cp.getAttribute("useAlertColors") != null;
		String type = cp.getAttribute("type");
		String mode = cp.getAttribute("mode");
		//legend config
		boolean visible= cp.getAttribute("visible") != null;
		String position = getTextValue(cp, "position");

		
		
		//Create a new Component element with the value read from the xml nodes
		VariableHandler e = new VariableHandler(ChartName,basetype,useAlertColors,type,
				mode,visible,position);
		
		return e;
	}

	private String getTextValue(Element ele, String tagName) {
		String textVal = null;
		NodeList nl = ele.getElementsByTagName(tagName);
		if(nl != null && nl.getLength() > 0) {
			Element el = (Element)nl.item(0);
			textVal = el.getFirstChild().getNodeValue();
		}

		return textVal;
	}
	
	
	@SuppressWarnings("unused")
	private int getIntValue(Element ele, String tagName) {
		//in production application you would catch the exception
		return Integer.parseInt(getTextValue(ele,tagName));
	}

	private void printData() {
		System.out.println("No of MainButton'" + component.size() + "'." 
				+ "\n" +"MainButton Details: ");
		Iterator<VariableHandler> it = component.iterator();
		while(it.hasNext()) {
			System.out.println(it.next().toString());
		}
	}
	
	public static void main(String[] args){
		//create an instance
		OptWindow optwin = new OptWindow();
		
		//call run 
		optwin.run();
	}
	
	

}


Java:
public class VariableHandler {


	public String getPosition() {
		return position;
	}

	public void setPosition(String position) {
		this.position = position;
	}

	public boolean isVisible() {
		return visible;
	}

	public void setVisible(boolean visible) {
		this.visible = visible;
	}

	public String getChartName() {
		return ChartName;
	}

	public String getBasetype() {
		return basetype;
	}

	public boolean isUseAlertColors() {
		return useAlertColors;
	}

	public String getType() {
		return type;
	}

	public String getMode() {
		return mode;
	}

	public void setChartName(String chartName) {
		ChartName = chartName;
	}

	public void setBasetype(String basetype) {
		this.basetype = basetype;
	}

	public void setUseAlertColors(boolean useAlertColors) {
		this.useAlertColors = useAlertColors;
	}

	public void setType(String type) {
		this.type = type;
	}

	public void setMode(String mode) {
		this.mode = mode;
	}


	private String ChartName;
	private String basetype;
	private boolean useAlertColors;
	private String type;
	private String mode;
	private boolean visible;
	private String position;
	
	public VariableHandler(){
		
	}

	public VariableHandler( String ChartName,String basetype, boolean useAlertColors, 
			String type, String mode, boolean visible, String position) {
		this.ChartName = ChartName;
		this.basetype = basetype;
		this.useAlertColors = useAlertColors;
		this.type = type;
		this.mode = mode;
		this.visible = visible;
		this.position = position;	
	}


	/**
	 * Test Area 
	 */
	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append("ChartName:" + getChartName());
		sb.append("\n");
		sb.append("basetype: " + getBasetype());
		sb.append("\n");
		sb.append("useAlertColors: " + isUseAlertColors());
		sb.append("\n");
		sb.append("type: " + getType());
		sb.append("\n");
		sb.append("mode: " + getMode());
		sb.append("\n");
		
		sb.append("visible: " + isVisible());
		sb.append("\n");
		sb.append("positon: " + getPosition());
		sb.append("\n");
		
		
		
		return sb.toString();
		}

	
}


[XML]<?xml version="1.0" encoding="UTF-8"?>

<OptimISEGuiModel>
<MainButton>
<ChartName>LOC</ChartName>
<charttype basetype="Cartesian">
<chartrenderer useAlertColors="true" type="LineChart" mode="LineChart"> </chartrenderer></charttype>
<legend visible="true" position="right" background="white" foreground="" />
<labelrenderer color="" offset= "" rotation= "" visible=""/>
<datasetlegend match="" display="Arch" showValue="true" showStart=" (total: " showEnd=")"/>
<graph name="Arch" value="50" color="0x808080"/>
<datasetlegend match="" display="Other" showValue="true" showStart= " (total: " showEnd=")" />
<graph name="Other" value="50" color="0xCC3333"/>
<datasetlegend match="" display="Model" showValue="true" showStart=" (total: " showEnd=")"/>
<graph name="Model" value="50" color="0x0000FF"/>
<datasetlegend match="" display="App" showValue="true" showStart=" (total: " showEnd=")" />
<graph name="App" value="50" color="0x009F6B"/>

</MainButton>

<MainButton>
<!-- Sir Statistics Window -->
<ChartName>Sir Statistics</ChartName>
<charttype basetype="Cartesian">
<chartrenderer useAlertColors="true" type="BarChart" mode="BarChart"> </chartrenderer></charttype>
<legend visible="true" position="right" background="white" foreground=""/>


</MainButton>

<MainButton>
<!-- Test Statistics Window -->
<ChartName>Test Statistics</ChartName>
<charttype basetype="Cartesian">
<chartrenderer useAlertColors="true" type="LineChart" mode="LineChart"> </chartrenderer></charttype>
<legend visible="true" position="right" background="white" foreground=""/>
</MainButton>

<MainButton>
<!-- Issue Window -->
<ChartName>Issue Overview</ChartName>
<charttype basetype="Cartesian">
<chartrenderer useAlertColors="true" type="BarChart" mode="BarChart"> </chartrenderer></charttype>
<legend visible="true" position="right" background="white" foreground=""/>
</MainButton>

<MainButton>
<ChartName>Rappidd</ChartName>
<!-- Rappidd Window -->
<charttype basetype="Cartesian">
<chartrenderer useAlertColors="true" type="BarChart" mode="BarChart"> </chartrenderer></charttype>
<legend visible="true" position="right" background="white" foreground=""/>
</MainButton>
</OptimISEGuiModel>
[/XML]
 
Ich würde wo immer es geht, eine XML Schema Datei nutzen, oder zur Not selbst erstellen und ein XML-Framework, sei es jetzt EMF oder JAXB nutzen, um XML zu lesen, zu verarbeiten und zu schreiben. Da ich "noch" nicht so Firm mit EMF bin, würde ich das mit JAXB machen (ist schon in Java 6 drin). Du hast dann keine Probleme beim Suchen von Elementen, Attributen und so weiter --> weniger Streß, du brauchst weniger Kopfschmerztabletten und Beruhigungsmittel 😉 (Ironie)
 
[JAVA=78]String basetype = cp.getAttribute("basetype");
[/code]

wird auf MainButton aufgerufen, sollte aber auf dem child charttype aufgerufen werden

[JAVA=80] String type = cp.getAttribute("type");
String mode = cp.getAttribute("mode");
[/code]

wird wieder auf MainButton aufgerufen, sollte aber auf dem "Enkel" chartrenderer aufgerufen werden.

[JAVA=84] String position = getTextValue(cp, "position");
[/code]
wird auf MainButton aufgerufen, sollte aber auf dem child legend aufgerufen werden

Zeile 83 ist auch nicht das wahre. Liefert true egal was das XML bietet. Und versucht auf visible von MainButton zuzugreifen, anstatt auf legend.
 
Hallo zusammen,

ich habe meine Parser mit SAX umgeschrieben, dummerweise habe ich auch hier Schwierigkeiten.
Ich kriege keine Fehlermeldung und ich sehe nichts an der Konsole. Was mache ich falsch??
Kann bitte jemand mir weiter Hilfen?
[XML]
<?xml version="1.0" encoding="UTF-8"?>

<OptimISEGuiModel>
<MainButton>
<ChartName>LOC</ChartName>

<charttype value="Cartesian">
<chartrenderer>
<useAlertColors value="true" />
<type value="LineChart" />
<mode value="LineChart" />
</chartrenderer>
</charttype>

<legend>
<visible value="true" />
<position value="right" />
<background value="white" />
<foreground value="" />
</legend>

<labelrenderer>
<color value="" />
<offset value="" />
<rotation value="" />
<visible value="" />
</labelrenderer>

<datasetlegend>
<match value="" />
<display value="Arch" />
<showValue value="true" />
<showStart value="(total: " />
<showEnd value=")" />
</datasetlegend>

<graph>
<name value="Arch" />
<value value="50" />
<color value="0x808080" />
</graph>

<datasetlegend>
<match value="" />
<display value="Other" />
<showValue value="true" />
<showStart value=" (total: " />
<showEnd value=")" />
</datasetlegend>

<graph>
<name value="Other" />
<value value="50" />
<color value="0xCC3333" />
</graph>

<datasetlegend>
<match value="" />
<display value="Model" />
<showValue value="true" />
<showStart value=" (total: " />
<showEnd value=")" />
</datasetlegend>

<graph>
<name value="Model" />
<value value="50" />
<color value="0x0000FF" />
</graph>

<datasetlegend>
<match value="" />
<display value="App" />
<showValue value="true" />
<showStart value=" (total: " />
<showEnd value=")" />
</datasetlegend>


<graph>
<name value="App" />
<value value="50" />
<color value="0x009F6B" />
</graph>

</MainButton>
</OptimISEGuiModel>
[/XML]


Java:
public class MainButton {
	public String getMainButton() {
		return MainButton;
	}

	public String getChartName() {
		return ChartName;
	}

	public String getCharttype() {
		return charttype;
	}

	public String getChartrenderer() {
		return chartrenderer;
	}

	public String getLegend() {
		return legend;
	}

	public String getLabelrenderer() {
		return labelrenderer;
	}

	public String getDatasetlengend() {
		return datasetlengend;
	}

	public String getGraph() {
		return graph;
	}

	public void setMainButton(String mainButton) {
		MainButton = mainButton;
	}

	public void setChartName(String chartName) {
		ChartName = chartName;
	}

	public void setCharttype(String charttype) {
		this.charttype = charttype;
	}

	public void setChartrenderer(String chartrenderer) {
		this.chartrenderer = chartrenderer;
	}

	public void setLegend(String legend) {
		this.legend = legend;
	}

	public void setLabelrenderer(String labelrenderer) {
		this.labelrenderer = labelrenderer;
	}

	public void setDatasetlengend(String datasetlengend) {
		this.datasetlengend = datasetlengend;
	}

	public void setGraph(String graph) {
		this.graph = graph;
	}

	private String MainButton;
	private String ChartName;
	private String charttype;
	private String chartrenderer;
	private String legend;
	private String labelrenderer;
	private String datasetlengend;
	private String graph;
	

////////////////////////////////////////////
	public class Charttype{
		
		public Charttype(){
				this.charttype= charttype;

		}
		public String getCharttype() {
			return charttype;
		}

		public void setCharttype(String charttype) {
			this.charttype = charttype;
		}

		private String charttype;
	
////////////////////////////////////////////

		public class Chartrenderer{
			
			public Chartrenderer(){
				this.useAlertColors = useAlertColors;
				this.type = type;
				this.mode = mode;
			}
		
			public String getUseAlertColors() {
				return useAlertColors;
			}
			public String getType() {
				return type;
			}
			public String getMode() {
				return mode;
			}
			public void setUseAlertColors(String useAlertColors) {
				this.useAlertColors = useAlertColors;
			}
			public void setType(String type) {
				this.type = type;
			}
			public void setMode(String mode) {
				this.mode = mode;
			}
			private String useAlertColors;
			private String type;
			private String mode;
			
			public String toString(){
				return this.useAlertColors + "\n" + this.type + "\n" + this.mode;
			}
		}
		
		public String toString(){
			return this.charttype;
		}
	}
////////////////////////////////////////////
	public class legend{
		
		public legend(){
			this.visible = visible;
			this.position = position;
			this.background = background;
			this.forground = forground;
		}
		public String getVisible() {
			return visible;
		}
		public String getPosition() {
			return position;
		}
		public String getBackground() {
			return background;
		}
		public String getForground() {
			return forground;
		}
		public void setVisible(String visible) {
			this.visible = visible;
		}
		public void setPosition(String position) {
			this.position = position;
		}
		public void setBackground(String background) {
			this.background = background;
		}
		public void setForground(String forground) {
			this.forground = forground;
		}
		private String visible;
		private String position;
		private String background;
		private String forground;
		
		public String toString(){
			return this.visible + "\n" + this.position + "\n" + this.background + "\n" + this.forground;
		}
		
		
	}
////////////////////////////////////////////

	public class labelrenderer{
		
		public labelrenderer(){
			this.color = color;
			this.offset = offset;
			this.rotation = rotation;
			this.visible = visible;
		}
		public String getColor() {
			return color;
		}
		public String getOffset() {
			return offset;
		}
		public int getRotation() {
			return rotation;
		}
		public String getVisible() {
			return visible;
		}
		public void setColor(String color) {
			this.color = color;
		}
		public void setOffset(String offset) {
			this.offset = offset;
		}
		public void setRotation(int rotation) {
			this.rotation = rotation;
		}
		public void setVisible(String visible) {
			this.visible = visible;
		}
		private String color;
		private String offset;
		private int rotation;
		private String visible;
		
		
		public String toString(){
			return this.color +"\n"+ this.offset +"\n"+ this.rotation +"\n"+ this.visible;
		}
	}
////////////////////////////////////////////

	public class datasetlegend{  // this is a list which repeats itself manytime unter Mainbutton
		
		public datasetlegend(){
			this.match = match;
			this.display = display;
			this.showValue = showValue;
			this.StartValue = StartValue;
			this.showEnd = showEnd;
		}
		
		public String getMatch() {
			return match;
		}
		public String getDisplay() {
			return display;
		}
		public String getShowValue() {
			return showValue;
		}
		public String getStartValue() {
			return StartValue;
		}
		public String getShowEnd() {
			return showEnd;
		}
		public void setMatch(String match) {
			this.match = match;
		}
		public void setDisplay(String display) {
			this.display = display;
		}
		public void setShowValue(String showValue) {
			this.showValue = showValue;
		}
		public void setStartValue(String startValue) {
			StartValue = startValue;
		}
		public void setShowEnd(String showEnd) {
			this.showEnd = showEnd;
		}
		private String match;
		private String display;
		private String showValue;
		private String StartValue;
		private String showEnd;
		
		public String toString(){
			return this.match + "\n" + this.display + "\n" + this.showValue + "\n" + this.StartValue + "\n" + this.showEnd;
		}
	}
////////////////////////////////////////////

	public class graph{		// this is a list which repeats itself manytime unter Mainbutton
		
		public graph(){
			
			this.name = name;
			this.value = value;
			this.color = color;
		}
		public String getName() {
			return name;
		}
		public String getValue() {
			return value;
		}
		public String getColor() {
			return color;
		}
		public void setName(String name) {
			this.name = name;
		}
		public void setValue(String value) {
			this.value = value;
		}
		public void setColor(String color) {
			this.color = color;
		}
		private String name;
		private String value;
		private String color;
		public String toString(){
			return this.name +"\n"+ this.value +"\n"+ this.color;
		}
	}
	
	public String toString(){
		StringBuffer sb = new StringBuffer();
		sb.append("ChartName:" + getChartName());
		sb.append("ChartType:" + getCharttype());
		sb.append("Chartrenderer:" + getChartrenderer());
		sb.append("Legend: " + getLegend());
		sb.append("Labelrenderer:" + getLabelrenderer());
		sb.append("DatasetLegend:" + getDatasetlengend());
		sb.append("graph:" + getGraph());
		
		
		return ChartName + chartrenderer + charttype + datasetlengend + graph + labelrenderer + legend + MainButton;			
	}

	
}


Java:
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 GuiMain {

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] argv) throws Exception {

//	     OptWindow optwin = new OptWindow();
//         optwin.run();
		try {

			// XMLReader erzeugen
			XMLReader xmlReader = XMLReaderFactory.createXMLReader();
			// Pfad zur XML Datei
			FileReader reader = new FileReader("OptGuiModel.xml");
			InputSource inputSource = new InputSource(reader);
			// PersonenContentHandler wird übergeben
			xmlReader.setContentHandler(new VariableContentHandler());
			// Parsen wird gestartet
			xmlReader.parse(inputSource);
			
			
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		}

	}
}


Java:
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 VariableContentHandler implements ContentHandler {
	
	private ArrayList<MainButton> allComponent = new ArrayList<MainButton>();
	private String actualValue;
	private MainButton mainButton;
	
	// actualValue die eingelsen wird, soll in eine Zwischenvariable gespeichert werden
	public void characters(char[] ch, int start, int length) throws SAXException {
	actualValue = 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("MainButton"));
	      // Neue component erzeugen
		 mainButton = new MainButton();
	}
	
	 
	// Methode wird aufgerufen wenn der Parser zu einem End-Tag kommt
	  public void endElement(String uri, String localName, String qName)
	      throws SAXException {
		  
		  if(localName.equals("ChartName")){
			  mainButton.setChartName(actualValue);
		  }

		  if(localName.equals("charttype")){
				 mainButton.setCharttype(actualValue);
				}
		  if(localName.equals("chartrenderer")){
			  mainButton.setChartrenderer(actualValue);
			  
			  // ich will die weitere elemente hier ausgeben wieso kann ich hier nicht zugreifen???
		  }

		  if(localName.equals("legend")){
			  mainButton.setLegend(actualValue);
		  }

		  if(localName.equals("labelrenderer")){
			  mainButton.setLabelrenderer(actualValue);
		  }

		  if(localName.equals("datasetlegend")){
			  mainButton.setDatasetlengend(actualValue);
		  }
		  
		  if(localName.equals("graph")){
			  mainButton.setGraph(actualValue);
		  }
		  
		  if(localName.equals("datasetlegend")){
			  mainButton.setDatasetlengend(actualValue);
		  }
	  }
	
	
	public void endDocument() throws SAXException {
		
	}

	@Override
	public void endPrefixMapping(String arg0) throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void ignorableWhitespace(char[] arg0, int arg1, int arg2)
			throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void processingInstruction(String arg0, String arg1)
			throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setDocumentLocator(Locator arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void skippedEntity(String arg0) throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void startDocument() throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void startPrefixMapping(String arg0, String arg1)
			throws SAXException {
		// TODO Auto-generated method stub
		
	}

}
 

Zurück
Oben