Sax Parser - how to add new nested elements
Hello,
I am quite new to Java and Sax Parser. I have spent the last couple of days trying to figure this out.
I have an xml file - - part sample below. I am using Sax Parser to parse the file. I am now trying to add new elements to the xml file along with associated data. I am confused as to how to add the elements.
Code:
<?xml version="1.0" encoding="UTF-8"?>
<defaultChartMeasure refDataItem="Speed">
<chartLabel>
<chartContents>
<chartTextItem>
<dataSource>
<staticValue>Speed</staticValue>
</dataSource>
<conditionalDataSources refVariable="Report Language">
<conditionalDataSource refVariableValue="en">
<staticValue>Speed</staticValue>
</conditionalDataSource>
<conditionalDataSource refVariableValue="pt-br">
<staticValue>Velocidade</staticValue>
</conditionalDataSource>
</conditionalDataSources>
</chartTextItem>
</chartContents>
</chartLabel>
</defaultChartMeasure>
I want to add elements like the below but with different values
Code:
<conditionalDataSource refVariableValue="pt-br">
<staticValue>Velocidade</staticValue>
</conditionalDataSource>
Here is my main class file
Code:
//below is all the event handlers that should be in their own class files
public void printData(){
System.out.println("report size is " + reportl.size());
for (report tmpR : reportl){
System.out.println(tmpR.toString());
}
}
//event handler
@Override
public void startElement(String s,String s1, String elementName, Attributes attributes)throws SAXException{
int length = attributes.getLength();
if(elementName.equalsIgnoreCase("conditionalDataSource")){
inConditionalDataSource=true;
//set the tempval to null so that their is no previous data in the variable
String tempValue ="";
// print out the attributes of the the element "conditionalDataSource"
for (int i=0; i<length; i++) {
// Get names and values for each attribute
String name = attributes.getQName(i);
String value = attributes.getValue(i);
// System.out.println("elementName= " +name);
System.out.println("elementValue= " + value);
}
//create a new instance of conditionalDataSource
conditionalDataSource = new defaultChartMeasure();
// tempValue.setType(attributes.getValue("type"));
}
}
@Override
public void endElement(String s, String s1,String elementName)throws SAXException{
//end element always goes to the end of the document REGARDLESS
if(elementName.equalsIgnoreCase("ConditionalDataSource")){
//adding it to the list
// myempls.add(tempemp);
inConditionalDataSource = false;
}
//now we have to loop through the rest of the nodes in the element
else if(elementName.equalsIgnoreCase("staticValue")){
// tmpValue.setStaticValue(tmpValue);
}
}
@Override
public void characters(char ch[],int start, int length) throws SAXException{
tmpValue = new String(ch,start,length);
}
Here is the class file for adding the elements but it is not complete
Code:
public class defaultChartMeasure {
private String refDataItem;
private String staticValue;
private String refVariable;
//creating the constructor
public defaultChartMeasure(){
setRefDataItem(null);
setRefVariable(null);
setStaticValue(null);
}
//use void for set ONLY and string etc for get
public void setRefDataItem(String refDataItem){
this.refDataItem =refDataItem;
}
public void setRefVariable(String refVariable){
this.refVariable = refVariable;
}
public void setStaticValue(String staticValue){
this.staticValue = "<staticValue></staticValue>";
}
}
Can someone explain to me what I need to do ?
Any advice will be greatly appreciated !
Re: Sax Parser - how to add new nested elements
I am no expert in this -- not by any means, but I always thought that a SAX parser was for, well, parsing, and that it is not a tool that is used for writing to an XML file.
Edit: Google showed me this: generating-xml-using-sax-and-java.
It states that a SAX parser is not used for generating XML, but a SAX "framework" can be used for this purpose. Though I must post the caveat that this is nothing I've ever worked with.
Re: Sax Parser - how to add new nested elements