Java and XML "templates"

  • Thread starter Michael Ransburg
  • Start date
M

Michael Ransburg

Hello!

I've got the following XML "template" (for example).

<personalData>
<address>
<name>$var1</name>
<zip>$var2</name>
</address>
<telephone type=$var3>$var4</telephone>
</personalData>


Is there an efficient way to replace the $var* variables by values
using Java? If not, what is an efficient way to work with such
templates?

I'd imagine something like the following:
Template t = new Template("template.xml");
t.var1 = "Michael";
t.var2 = 81825;
t.var3 = "Mobile Phone";
t.var4 = 12345678;
Document d = t.serialize();


Any help would be greatly appreciated!


Best regards
Michael

ps: Sorry if this message appears twice.
 
C

Collin VanDyck

----- Original Message -----
From: "Michael Ransburg" <[email protected]>
Newsgroups: comp.lang.java.programmer
Sent: Monday, February 09, 2004 5:32 PM
Subject: Java and XML "templates"

Hello!

I've got the following XML "template" (for example).

<personalData>
<address>
<name>$var1</name>
<zip>$var2</name>
</address>
<telephone type=$var3>$var4</telephone>
</personalData>


Is there an efficient way to replace the $var* variables by values
using Java? If not, what is an efficient way to work with such
templates?

I'd imagine something like the following:
Template t = new Template("template.xml");
t.var1 = "Michael";
t.var2 = 81825;
t.var3 = "Mobile Phone";
t.var4 = 12345678;
Document d = t.serialize();


Any help would be greatly appreciated!


Best regards
Michael

ps: Sorry if this message appears twice.

The person who originally replied to your first message was correct.

What you are going to need to do will be something like this:

1. Read in the XML using a parser and build a Document.
2. Run the XML through a transformer; the transformer that you write will
"catch" these $var1 and $var2 character sequences and you will be able to
substitute real values back into the document.
3. Optionally, serialize your Document to a String or some other data type.

You should start here:

http://java.sun.com/xml/jaxp/dist/1.0.1/docs/tutorial/TOC.html

And go through the tutorials, if you want to have a good understanding of
what you are doing. The problem you describe is not "easily" solved without
a basic knowledge of how to parse and manipulate XML documents.

I think that the solution you create will be created more quickly, and more
correctly, if you first go through the java XML tutorial first.

good luck!
Collin
 
B

Berlin Brown

Michael said:
Hello!

I've got the following XML "template" (for example).

<personalData>
<address>
<name>$var1</name>
<zip>$var2</name>
</address>
<telephone type=$var3>$var4</telephone>
</personalData>


Is there an efficient way to replace the $var* variables by values
using Java? If not, what is an efficient way to work with such
templates?

I'd imagine something like the following:
Template t = new Template("template.xml");
t.var1 = "Michael";
t.var2 = 81825;
t.var3 = "Mobile Phone";
t.var4 = 12345678;
Document d = t.serialize();


Any help would be greatly appreciated!


Best regards
Michael

ps: Sorry if this message appears twice.

I am not sure what your vision of xml is, but the simplest vision would
be screw the template and use code, and use for example JDOM, there are
a billion other ways to add data to your document but the jdom
way(pseudo code of course).

org.blah.jdom.Element _root = new org.blah.jdom.Element("personalData")

org.blah.Document _doc = new Document(_root);

Element a = new Element("address");
Element n = new Element("name");
Element z = new Element("zip");
Element t = new Element("telephone");

_root.addContent(a);
a.addContent(n);
a.addContent(z);

and so on
and then output to whatever.

If you were thinking more complicated, and I dont think you were.

You could use XSLT(stylesheets) and with your template create an
inteface to a xml loader.

For example


<personalData>
<address>
<name>$var1</name>
<zip>$var2</name>
</address>
<telephone type=$var3>$var4</telephone>
</personalData>


--->>>> style-sheet --->>> generates an interface(.java file) hehe
you probably werent going this far of course.
 
M

Michael Ransburg

Thank you very much for all of your replies!

My application is based on a number (10-30) of small xml files (each
50-100 lines). I want to use this "template" principle because the xml
files will be rather dynamic, they will change in structure often and
new xml files might be added as well as old ones deleted.

That's why I do not want to use DOM or JDOM to "hard code" the
structure of each style sheet into my program. I'd much rather use
such templates for the style sheets and then just use a more or less
generic method inside my program to replace the $vars in each template
by the appropriate values. This would minimize the coding efford when
changes occour and at the same time one could easily see which
templates the application supports, without having to dig through the
source code.

I'll look at your suggestions. I did hope that there would be a more
direct way to work with such templates, but I guess reading the
template in and then going through it with SAX or another parser won't
be that hard :)

Thanks again for your suggestions and if you have things to add I'd be
really grateful. I'll check this thread regularly!

cheers
Mike
 
H

Harald Kirsch

I've got the following XML "template" (for example).

<personalData>
<address>
<name>$var1</name>
<zip>$var2</name>
</address>
<telephone type=$var3>$var4</telephone>
</personalData>


Is there an efficient way to replace the $var* variables by values
using Java? If not, what is an efficient way to work with such
templates?

java.util.regex

Well, "efficient" is a fuzzy concept. Does it mean your code must
be efficient because you have several billion templates to process?
In that case java.util.regex is not for you. If, however, we talk
about some thousand and you need to get the work done,
then do a search/replace with java.util.regex.

Harald.
 
M

Markos Charatzas

Have a look at Velocity (http://jakarta.apache.org/velocity/)

It works like this:

Code:
/**
* @param data a list with all the objects you want to display
* @param contextName the name of the variable in the xml file which
will hold the list
* @param templateName the name of the file
* @return
*/
public void toXML(List data, String contextName, String templateName)
{
Template template;
StringWriter xmlWriter = new StringWriter();

VelocityContext context = new VelocityContext();

context.put(contextName, data );

try
{
template = Velocity.getTemplate(templateFile);
template.merge( context, xmlWriter );
}
catch (ResourceNotFoundException e)
{
e.printStackTrace();
}
catch (ParseErrorException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
}

The XML file could look like this:

#foreach($personalData in $personalDatas)
<personalData>
<address>
<name>personalData.getAddress().getName()</name>
<zip>personalData.getAddress().getZip()</zip>
</address>
<telephone
type=personalData.getTelephone().getType()>personalData.getTelephone().toString()</telephone>
</personalData>
#end

Let us know if u have any probs.
 
B

Bert Schader

Michael said:
I've got the following XML "template" (for example).

<personalData>
<address>
<name>$var1</name>
<zip>$var2</name>
</address>
<telephone type=$var3>$var4</telephone>
</personalData>

Is there an efficient way to replace the $var* variables by values
using Java? If not, what is an efficient way to work with such
templates?

If you are actually not interested in the document structure, but
just need to replace the vars with actual values, you could just
read the xml as string (without any parsing) and replace the
variables using something like this
http://www.javapractices.com/Topic80.cjp

hope that helps,
Bert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top