Avatar of annie613
annie613

asked on 

For Loop Logic

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>

Programming Languages-OtherJava

Avatar of undefined
Last Comment
annie613

8/22/2022 - Mon