Results 1 to 3 of 3
- 06-08-2012, 01:28 AM #1
Member
- Join Date
- Jul 2011
- Posts
- 32
- Rep Power
- 0
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.
Java 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
Java Code:<conditionalDataSource refVariableValue="pt-br"> <staticValue>Velocidade</staticValue> </conditionalDataSource>
Here is my main class file
Here is the class file for adding the elements but it is not completeJava 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); }
Java 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.Last edited by Fubarable; 06-08-2012 at 02:26 AM.
- 06-11-2012, 12:54 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 32
- Rep Power
- 0
Similar Threads
-
Print the sum of elements, determined by zero elements
By Dimitri in forum New To JavaReplies: 3Last Post: 10-19-2011, 11:42 PM -
[Semi-Beginner] (nested loops) What's wrong with my code? (nested loops)
By Solarsonic in forum New To JavaReplies: 20Last Post: 03-22-2011, 04:02 AM -
Using HTMLEditorKit.Parser to parse <embed> elements
By GlideKensington in forum New To JavaReplies: 0Last Post: 03-29-2009, 11:44 PM -
How to use Inner bean definitions via nested bean elements
By Java Tip in forum Java TipReplies: 0Last Post: 03-30-2008, 10:03 AM -
How to use Inner bean definitions via nested bean elements
By JavaBean in forum Java TipReplies: 0Last Post: 09-26-2007, 08:36 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks