Results 1 to 3 of 3
  1. #1
    alexandra12 is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default 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
    Java 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

    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 !

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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 03:26 AM.

  3. #3
    alexandra12 is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default Re: Sax Parser - how to add new nested elements

    Thanks Fubarable

Similar Threads

  1. Replies: 3
    Last Post: 10-20-2011, 12:42 AM
  2. Replies: 20
    Last Post: 03-22-2011, 05:02 AM
  3. Using HTMLEditorKit.Parser to parse <embed> elements
    By GlideKensington in forum New To Java
    Replies: 0
    Last Post: 03-30-2009, 12:44 AM
  4. Replies: 0
    Last Post: 03-30-2008, 11:03 AM
  5. Replies: 0
    Last Post: 09-26-2007, 09:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •