You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by René Jensen <lu...@tbkol.dk> on 2001/03/01 14:27:29 UTC
How do I put value into an element tag?
Hi
I know how to parse a xml-document into a DOM-tree, and traverse this tree.
Now I have 2 questions
1) I wanna fill out the empty tags i might have fx.
<root>
<tag1></tag1>
</root>
should be:
<root>
<tag1>Some new text here</tag1>
</root>
2) I wanna add childelements to my excisting elements fx.
<root>
<tag1></tag1>
</root>
becomes:
<root>
<tag1>
</tag2>
</tag3>
</tag1>
</root>
I wanna do this in the DOM-tree, because I have to do it several times,
when my tree is done, I can return the tree (as a xml-document) with all the
data and new tags...
I have searched the net, but I can only find examples of how to traverse the
tree, but not a single example of how to add a new branch to my DOM-tree
Any help / hint would be appriciated.
If you have an example I would be very glad.
_\|/_
(@ @)
---oOOo-(_)-oOOo---
René Jensen
lundeman@tbkol.dk
Re: How do I put value into an element tag?
Posted by Leonardo Battagli <ba...@sdb.it>.
At 14.27 01/03/01 +0100, you wrote:
>Hi
>
>I know how to parse a xml-document into a DOM-tree, and traverse this tree.
>Now I have 2 questions
>
>1) I wanna fill out the empty tags i might have fx.
>
><root>
> <tag1></tag1>
></root>
>
>should be:
>
><root>
> <tag1>Some new text here</tag1>
></root>
>
>2) I wanna add childelements to my excisting elements fx.
>
><root>
> <tag1></tag1>
></root>
>
>becomes:
>
><root>
> <tag1>
> </tag2>
> </tag3>
> </tag1>
></root>
>
>I wanna do this in the DOM-tree, because I have to do it several times,
>when my tree is done, I can return the tree (as a xml-document) with all the
>data and new tags...
I can help you with this method that merges 2 dom objects,
the first is
* <?xml version="1.0"?>
* <ROWSET>
* <ROW>
<TEMP>
</TEMP>
* </ROW>
* </ROWSET>
the second is
<ROWSET>
<ROW num=1>
<NOME>Leo</NOME>
<COGNOME>Batta</COGNOME>
<TEL>347123</TEL>
</ROW>
</ROWSET>
if you execute the methos in this way
BeanDom.CostruisciDOM(dom1, dom2, ROWSET, DATI);
you'll have back an xmldocument like this one
<?xml version="1.0"?>
<ROWSET>
<ROW num=1>
<TEMP>Leo</TEMP>
</ROW>
<DATI>
<NOME>Max</NOME>
<COGNOME>Conte</COGNOME>
<TEL>123347</TEL>
</DATI>
</ROWSET>
Hope it helps in some way
Leonardo
/**
* Dati 2 oggetti DOM aggiunge al nodo ROWSET uno o piu' nodi definiti dal
* tag NEWNODO
* N: Restituisce un oggetto DOM di quewcsto tipo
* <?xml version="1.0"?>
* <ROWSET>
* <ROW>
* </ROW>
* <ROW>
* </ROW>
* <ROW>
* </ROW>
* <DATA>
* </DATA>
* <AREAGEO>
* </AREAGEO>
* <VIATERRA>
* </VIATERRA>
* <VIAMARE>
* </VIAMARE>
* </ROWSET>
*/
public XMLDocument CostruisciDOM(XMLDocument dompadre, XMLDocument
domfiglio, String nodopadre, String nodofiglio)
{
//Prendo la lista dei figli del DOMPADRE
NodeList listafiglipadre = dompadre.getChildNodes();
//MI memorizzo il nodo zero per aggiungervi un nodo 'ROWLIST'
Node nodo = listafiglipadre.item(0);
if (nodo == null) System.out.println("NODO BASE E' NULL");
//Mi creo un 'nodobase' di tipo NODE
Node nodobase = nodo.getOwnerDocument().getDocumentElement();
//Mi creo un 'rootbase' di tipo DOCUMENT
Document rootbase = nodo.getOwnerDocument();
//entro nel primo nodo (che è la ROOT='ROWSET')
nodo = listafiglipadre.item(1);
//mi memorizzo il nodo rowset dove dovrò appendere le ROWS del dom2
//questo nodo viene utilizzato per l'inserimento ed il merge con
altri dom
Node aggiungerequi = nodo.getOwnerDocument().getDocumentElement();
//Prendo la lista dei figli del DOMFIGLIO
NodeList listafiglifiglio = domfiglio.getChildNodes();
//entro nel primo nodo (che è la ROOT='ROWSET')
nodo = listafiglifiglio.item(1);
//Prendo la lista dei figli di 'ROWSET' cioè una sfilza di 'ROWS'
NodeList listarowsfiglio = nodo.getChildNodes();
//Creo un elemento 'VIAMARE'
//Element viamare = (Element)rootbase.createElement("VIAMARE");
Element nomenewnodo =
(Element)rootbase.createElement(nodofiglio.toString());
//lo aggiungo alla root del DOM1
nodobase.appendChild(nomenewnodo);
//Ciclo fintanto ci sono ROWS nel DOMFIGLIO
//Per ogni ROWS devo creare un nuovo nodo all'interno del NODOFIGLIO
//Es:
// <VIAMARE>
// <OPERATORE num="1">
// <NOME>Leo</NOME>
// <COGNOME>Batta</COGNOME>
// <TELEFONO>0347</TELEFONO>
// </OPERATORE>
// <NOME>Leo</NOME>
// <COGNOME>Batta</COGNOME>
// <TELEFONO>0347</TELEFONO>
// <OPERATORE num="2">
// </OPERATORE>
// <OPERATORE num="3">
// <NOME>Leo</NOME>
// <COGNOME>Batta</COGNOME>
// <TELEFONO>0347</TELEFONO>
// </OPERATORE>
// </VIAMARE>
for (int i = 0; i < listarowsfiglio.getLength(); i++)
{
//Mi posizione sull'elemento i
nodo = listarowsfiglio.item(i);
//System.out.println("BeanDOM-QUANTI CAMPI PER OGNI
ROWS???"+nodo.getChildNodes().getLength());
//Creo un elemento 'ROW'
Element nomenodo = (Element)rootbase.createElement("ROW");
//lo aggiungo al NODO appena creato, cioè NODOFIGLIO
nomenewnodo.appendChild(nomenodo);
//adesso gli setto l'attributo per poi poter effetturare un ciclo
'FOR-EACH' dal file XSL
Integer k = new Integer(i+1);
nomenewnodo.setAttribute("num",k.toString());
NodeList datiinterni = nodo.getChildNodes();
for (int j = 0; j < datiinterni.getLength(); j++)
{
//Mi posizione sul primo parametro, ad esempio NOME
nodo = datiinterni.item(j);
//recupero il nome di tale nodo, es nomenodo = 'NOME'
String nomenodoint = nodo.getNodeName();
//Per recuperare il valore devo posizionarmi sul primo figlio
Node testo = nodo.getFirstChild();
String testovero = testo.getNodeValue();
String valorenodo = testovero;
//Creo un elemento 'nomenodoint', cioè NOME
Element dato = (Element)rootbase.createElement(nomenodoint);
//lo aggiungo al nodo ROW, cioè dentro al tag ROW
nomenodo.appendChild(dato);
//gli assegno 'valorenodo'
dato.appendChild( rootbase.createTextNode(valorenodo) );
}
}
//ATTENZIONEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
return dompadre;
}
>I have searched the net, but I can only find examples of how to traverse the
>tree, but not a single example of how to add a new branch to my DOM-tree
>
>Any help / hint would be appriciated.
>If you have an example I would be very glad.
>
> _\|/_
> (@ @)
>---oOOo-(_)-oOOo---
> René Jensen
> lundeman@tbkol.dk
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
>For additional commands, e-mail: xerces-j-user-help@xml.apache.org
==============================================
Lulli Informatica S.r.l.
http://www.lulliinformatica.com
----------------------------------------------
Leonardo Battagli: Java/XML Developer at battagli@sdb.it
==============================================
Out of the office email address: leonardobattagli@usa.net
==============================================