Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Fill Data Into An XML Template With VTD-XML

DZone's Guide to

Fill Data Into An XML Template With VTD-XML

·
Free Resource
This example shows you how to edit an XML template to fill in the data to generate an output XML file.


/* This is the template

  
    
   
        
                          
        
                  
    
        
           
    
        
                  
    
        
            
    
        
            
    
    
   
    
   
        
                          
        
                  
    
        
           
    
        
                  
    
        
            
    
        
            
    
    
   
    
   
        
                          
        
                  
    
        
           
    
        
                  
    
        
            
    
        
            
    
    
    
    
   
        
                          
        
                  
    
        
           
    
        
                  
    
        
            
    
        
            
    
     
   

  
*/

/*  Output XML file

  
    
   
        
    Empire Burlesque      
        
    
     Bob Dylan     
    
        
    
     USA    
    
        
    
     Columbia      
    
        
    
     10.9    
    
        
    
     1985    
    
    
   
    
   
        
    Still Got the Blues   
        
    
     Gary More     
    
        
    
     UK     
    
        
    
     Virgin Records
    
        
    
     10.2    
    
        
    
     1990    
    
    
   
    
   
        
                          
        
                  
    
        
           
    
        
                  
    
        
            
    
        
            
    
    
    
    
   
        
                          
        
                  
    
        
           
    
        
                  
    
        
            
    
        
            
    
     
   

  
*/

import com.ximpleware.*;
import java.io.*;
public class editTemplate
{
	public static void main(String[] args) throws Exception
	{
		VTDGen vg = new VTDGen();
		FileOutputStream fos = new FileOutputStream("new_cd.xml");
		AutoPilot ap = new AutoPilot();
		ap.selectXPath("/CATALOG/CD");
		if (vg.parseFile("cd_Template.xml", false))
		{
			VTDNav vn = vg.getNav();
			ap.bind(vn);
			if (ap.evalXPath() == -1)
			{
				System.out.println("XPath eval failed");
				System.exit(0);
			}
			fillTemplate(vn, "Empire Burlesque", "Bob Dylan", "USA", "Columbia", 10.90, 1985);
			if (ap.evalXPath() == -1)
			{
				System.out.println("XPath eval failed");
				System.exit(0);
			}
			fillTemplate(vn, "Still Got the Blues", "Gary More", "UK", "Virgin Records", 10.20, 1990);
			// dump out the XML
			fos.write(vn.getXML().getBytes());
			fos.close();
		}
	}
	public static void fillTemplate(VTDNav vn,
			String title, String artist, String country,
			String company, double price, int year) throws NavException
	{

		int i = -1;
		if (vn.toElement(VTDNav.FIRST_CHILD))
		{
			vn.overWrite(vn.getText(), title.getBytes());
			vn.toElement(VTDNav.NEXT_SIBLING);
			vn.overWrite(vn.getText(), artist.getBytes());
			vn.toElement(VTDNav.NEXT_SIBLING);
			vn.overWrite(vn.getText(), country.getBytes());
			vn.toElement(VTDNav.NEXT_SIBLING);
			vn.overWrite(vn.getText(), company.getBytes());
			vn.toElement(VTDNav.NEXT_SIBLING);
			vn.overWrite(vn.getText(), (price + "").getBytes());
			vn.toElement(VTDNav.NEXT_SIBLING);
			vn.overWrite(vn.getText(), (year + "").getBytes());
		}
		vn.toElement(VTDNav.PARENT);
	}
}
Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}