Appending nodes to Elements

Hi,

I am trying to use DOM for creating an XML file based on a String array that I give on input.

My problem is that as I am iterating through the array and appending the Child for the Element, in the output XML I am not seeing them as separate entities.

Example:

If my input is a String array with the following: {myInput1, myInput2}

My current output looks like this:

<Result>

<Value>myInput1myInput2</value>

and what I need to get is

<Result>

<Value>myInput1</Value>

<Value>myInput2</Value>

This is how I am trying to add the nodes:

Element myResult = xmldoc.createElement("Result");

Element myValue = xmldoc.createElement("Value");

myResult.appendChild(myValue);

for(int i = 0 ; i < myArray.length ; i++)

{

Node actualValue = xmldoc.createTextNode(myArray);

myValue.appendChild(actualValue);

}

I am not sure if my approach is wrong, can anyone help with this?

Thanks!

[1078 byte] By [desimundaa] at [2007-11-26 22:48:49]
# 1

If you want more than one Value element, then you should write code that creates more than one Value element.

> I am not sure if my approach is wrong

I don't understand this statement. Your output is not what you want it to be. Therefore your code is wrong. What's so hard to understand about that?

DrClapa at 2007-7-10 12:08:52 > top of Java-index,Java Essentials,Java Programming...
# 2

>If you want more than one Value element, then you should write code that creates more than one Value element.

When I am getting an array input how can I dynamically create a separate node element for each array element?

> I don't understand this statement. Your output is not what you want it to be. Therefore your code is wrong. What's so hard to understand about that?

I am not getting compiler errors, so code is not wrong. That is why I said 'if my approach is wrong'. It means is there something wrong with the method/algorithm of creating nodes on separate lines. I hope you understand the difference now.

========================

Updated code to specify array elements that are being picked but still not output as expected

Element myResult = xmldoc.createElement("Result");

Element myValue = xmldoc.createElement("Value");

myResult.appendChild(myValue);

for(int i = 0 ; i < myArray.length ; i++)

{

Node actualValue = xmldoc.createTextNode(myArray);

myValue.appendChild(actualValue);

}

========================

desimundaa at 2007-7-10 12:08:52 > top of Java-index,Java Essentials,Java Programming...