You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xalan.apache.org by Joona Palaste <jo...@dmm.fi> on 2007/04/04 20:11:07 UTC

How to set variables in XML document?

How can I set variables in an XML document in Java?
Suppose I have the following XML document in a file on disk:

<?xml version="1.0" encoding="UTF-8"?>
<document>
  <entity type="normal" id="1">${foo}</entity>
</document>

Then I use an org.w3c.dom.DocumentBuilder to parse this into an
org.w3c.dom.Document. Let's say I store this Document in the variable
doc. How can I do something like this:

doc.setVariable("foo", "bar");

so the document the Document represents ends up as this?

<?xml version="1.0" encoding="UTF-8"?>
<document>
  <entity type="normal" id="1">bar</entity>
</document>

Joona Palaste


RE: How to set variables in XML document?

Posted by Robert Houben <Ro...@fusionware.net>.
You could also use an XPath to select "//*[contains(text(),'${foo}')]"
(select all elements with a text node that contains your variable) which
is also a Xalan-J feature.  You could then traverse the node-list that
comes back and do the substitutions yourself.

-----Original Message-----
From: Mukul Gandhi [mailto:gandhi.mukul@gmail.com] 
Sent: Wednesday, April 04, 2007 11:29 AM
To: Joona Palaste
Cc: xalan-j-users@xml.apache.org
Subject: Re: How to set variables in XML document?

This is a Xalan-J users forum. Your question seems to be related to,
how to use DOM API (with Xerces I guess). You should subscribe to
j-users@xerces.apache.org for that.

But I can try to answer your question.

If you have got a XML document wrapped in org.w3c.dom.Document, then
Document interface doesn't provide any method to do something like
doc.setVariable("foo", "bar"). I think XSLT will be more suitable for
this task. And you can use Xalan-J for that.

I think you have to traverse the DOM object, and reach to the
concerned text node (${foo} -- wrapped in org.w3c.dom.Node). Then you
can modify the value of this node.

On 4/4/07, Joona Palaste <jo...@dmm.fi> wrote:
> How can I set variables in an XML document in Java?
> Suppose I have the following XML document in a file on disk:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <document>
>  <entity type="normal" id="1">${foo}</entity>
> </document>
>
> Then I use an org.w3c.dom.DocumentBuilder to parse this into an
> org.w3c.dom.Document. Let's say I store this Document in the variable
> doc. How can I do something like this:
>
> doc.setVariable("foo", "bar");
>
> so the document the Document represents ends up as this?
>
> <?xml version="1.0" encoding="UTF-8"?>
> <document>
>  <entity type="normal" id="1">bar</entity>
> </document>
>
> Joona Palaste


-- 
Regards,
Mukul Gandhi

Re: How to set variables in XML document?

Posted by Mukul Gandhi <ga...@gmail.com>.
This is a Xalan-J users forum. Your question seems to be related to,
how to use DOM API (with Xerces I guess). You should subscribe to
j-users@xerces.apache.org for that.

But I can try to answer your question.

If you have got a XML document wrapped in org.w3c.dom.Document, then
Document interface doesn't provide any method to do something like
doc.setVariable("foo", "bar"). I think XSLT will be more suitable for
this task. And you can use Xalan-J for that.

I think you have to traverse the DOM object, and reach to the
concerned text node (${foo} -- wrapped in org.w3c.dom.Node). Then you
can modify the value of this node.

On 4/4/07, Joona Palaste <jo...@dmm.fi> wrote:
> How can I set variables in an XML document in Java?
> Suppose I have the following XML document in a file on disk:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <document>
>  <entity type="normal" id="1">${foo}</entity>
> </document>
>
> Then I use an org.w3c.dom.DocumentBuilder to parse this into an
> org.w3c.dom.Document. Let's say I store this Document in the variable
> doc. How can I do something like this:
>
> doc.setVariable("foo", "bar");
>
> so the document the Document represents ends up as this?
>
> <?xml version="1.0" encoding="UTF-8"?>
> <document>
>  <entity type="normal" id="1">bar</entity>
> </document>
>
> Joona Palaste


-- 
Regards,
Mukul Gandhi