Advertisement

07.24.2008 at 05:38AM PDT, ID: 23591801
[x]
Attachment Details

For Loop Logic

Asked by annie613 in Programming Languages, Java Programming Language

Tags: Java, IE 6

Hi, I am trying to figure out this loop logic.
I am pulling two attributes from a DB.
The Field Name and Field Value.

I want to create and XML String with the DB Field Name as an XML tag and place the DB Field Value inside the XML Tag.

I am able to get the field name and field value but I am having a hard time trying to work this logic.

This is what I want my end result to look like
      
<?xml version="1.0" encoding="UTF-8"?>
<xml xmlns:test="http://localhost/test">
      <test:XMLTest>
            <test:XMLTagTest>
                  <test:Name>MyName</test:Name>
                  <test:Email>name@email.com</test:Email>
                  <test:Date>07/24/2008</test:Date>
                  <test:Misc></test:Misc>
                  <test:Approve></test:Approve>
            </test:XMLTagTest>
      </test:XMLTest>
</xml>

Here is the code I am using but I am having a hard time with the for loop for the values
Any suggestions. Thanks in advance.

      String showNames[] = item.getAttrNames();// names of fields from DB
      String showValues[] = item.getAttrValues();//values in fields from DB
      String value = null;
 
      nodeHit = (ElementImpl)XMLHlper.createAndAddtestChildElement(docXml, nodeHits, "XMLTagTest","");
 
      for(int i = 0; i < showNames.length ; i++)
      {                              
            for(int j = 0; j < showValues.length ; j++)   //trying to figure this part out
            {
                value = showValues[j];
                System.out.println(value);
            }      
            
            XMLHlper.createAndAddtestChildElement(docXml, nodeHit, showNames[i], value);
            xml = XMLHlper.getXmlString(docXml);
 
      }
      
      System.out.println(xml);

Here is what I get when I run the Code
These are the values that are pulled from the DB, which are correct

value = MyName
value = name@email.com
value = 07/24/2008
value =
value =

BUT the XML String comes up with blank values

<?xml version="1.0" encoding="UTF-8"?>
<xml xmlns:test="http://localhost/test">
      <test:XMLTest>
            <test:XMLTagTest>
                  <test:Name></test:Name>
                  <test:Email></test:Email>
                  <test:Date></test:Date>
                  <test:Misc></test:Misc>
                  <test:Approve></test:Approve>
            </test:XMLTagTest>
      </test:XMLTest>
</xml>

Start Free Trial
[+][-]07.24.2008 at 05:49AM PDT, ID: 22078486

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Programming Languages, Java Programming Language
Tags: Java, IE 6
Sign Up Now!
Solution Provided By: ksivananth
Participating Experts: 1
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628