DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Reproducibility as a Competitive Edge: Why Minimal Config Beats Complex Install Scripts
  • Unveiling the Clever Way: Converting XML to Relational Data
  • Go Application Vulnerability Cheatsheet
  • The X-Factor: RAML With XML Format

Trending

  • Avoiding AI Debt as a Team
  • Beyond the Wall of Charts: How Advanced Observability Displaced the Telemetry Dashboard
  • The Self-Hosting Math Changed While We Were Still Arguing About Sovereignty
  • A Non-Expiring AKS Deployment Token Could Read Secrets Across Three Namespaces

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Data
  4. Fill Data Into An XML Template With VTD-XML

Fill Data Into An XML Template With VTD-XML

By 
Jimmy Zhang
·
Nov. 30, 09 · Code Snippet
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
3.6K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in
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);
	}
}
VTD-XML XML Template Data (computing)

Opinions expressed by DZone contributors are their own.

Related

  • Reproducibility as a Competitive Edge: Why Minimal Config Beats Complex Install Scripts
  • Unveiling the Clever Way: Converting XML to Relational Data
  • Go Application Vulnerability Cheatsheet
  • The X-Factor: RAML With XML Format

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook