converting vector to xml


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: converting vector to xml

  1. #1
    peter Guest

    converting vector to xml


    Input will be vector whose each element will itself be a vector.Length of
    vector of each element may vary for different calls.Only the selected element
    needs to be placed in proper order in the output.For ex. field2 is not required
    in the output.Also field4 is placed before field3.Below is the code that
    I tried to write.Can any one help me with better solution.

    The Input is vector :
    "[ [field1,f1_val1,f1_val2,f1_val3,f1_val4] ,[field2,f2_val1,f2_val2,f2_val3,f2_val4],

    ,[field3,f3_val1,f3_val2,f3_val3,f3_val4,f3_val5],
    ,[field4,f4_val1,f4_val2]] )

    Expected output is :
    < FIELD Row=1>
    <field1>f1_val</field1>
    <field4>f2_val1</filed4>
    <field3>f3_val1</field3>
    </FIELD>
    < FIELD Row=2>
    <field1>f1_va2</field1>
    <field4>f2_val2</filed4>
    <field3>f3_val2</field3>
    </FIELD>
    < FIELD Row=3>
    <field1>f1_va3</field1>
    <field3>f3_val3</field3>
    </FIELD>
    < FIELD Row=4>
    <field1>f1_val4</field1>
    <field3>f3_val4</field3>
    </FIELD>
    < FIELD Row=5>
    <field3>f3_val5</field3>
    </FIELD>

    Code:


    import java.util.*;
    import java.io.*;
    public class TEST
    {
    public TEST()
    {
    }
    public String getXXX( String fileName, Vector inVect )
    {
    StringBuffer row1 = new StringBuffer();
    StringBuffer row2 = new StringBuffer();
    StringBuffer row3 = new StringBuffer();
    StringBuffer row4 = new StringBuffer();
    StringBuffer row5 = new StringBuffer();
    StringBuffer allrows = new StringBuffer();

    try
    {

    for( int j = 0; j < inVect.size(); ++j )
    {

    Vector innerVect = (Vector)inVect.elementAt( j );
    String key = (String)innerVect.elementAt( 0 );
    if(( key.equalsIgnoreCase("field1")))
    {
    row1.append( "<" + key + ">");

    row1.append(innerVect.elementAt(1).toString());
    row1.append( "</" + key + ">");
    row2.append( "<" + key + ">");
    row2.append(innerVect.elementAt(2).toString());
    row2.append( "</" + key + ">");
    row3.append( "<" + key + ">");
    row3.append(innerVect.elementAt(3).toString());
    row3.append( "</" + key + ">");
    row4.append( "<" + key + ">");
    row4.append(innerVect.elementAt(4).toString());
    row4.append( "</" + key + ">");

    } //endif drop_ship_addr1
    else if (( key.equalsIgnoreCase("field4")))
    {
    row1.append( "<" + key + ">");

    row1.append(innerVect.elementAt(1).toString());
    row1.append( "</" + key + ">");
    row2.append( "<" + key + ">");
    row2.append(innerVect.elementAt(2).toString());
    row2.append( "</" + key + ">");


    } //endif

    else if (( key.equalsIgnoreCase("field3")))
    {
    row1.append( "<" + key + ">");

    row1.append(innerVect.elementAt(1).toString());
    row1.append( "</" + key + ">");
    row2.append( "<" + key + ">");
    row2.append(innerVect.elementAt(2).toString());
    row2.append( "</" + key + ">");
    row3.append( "<" + key + ">");
    row3.append(innerVect.elementAt(3).toString());
    row3.append( "</" + key + ">");
    row4.append( "<" + key + ">");
    row4.append(innerVect.elementAt(4).toString());
    row4.append( "</" + key + ">");
    row5.append( "<" + key + ">");
    row5.append (innerVect.elementAt(5).toString());
    row5.append( "</" + key + ">");

    } //endif drop_ship_addr3


    }

    allrows.append(row1.toString()) ;
    allrows.append(row2.toString());
    allrows.append(row3.toString());
    allrows.append(row4.toString());


    }
    catch( Exception e ) {
    StringWriter sw = new StringWriter();
    e.printStackTrace(new PrintWriter(sw));
    String theMessage = sw.toString();
    System.out.println( theMessage);
    };
    System.out.println( allrows.toString());
    return allrows.toString();
    }
    public static void main(String[] args)
    {
    TEST pj2 = new TEST();

    pj2.getXXX( "output.txt",[ [field1,f1_val1,f1_val2,f1_val3,f1_val4] ,[field2,f2_val1,f2_val2,f2_val3,f2_val4],

    ,[field3,f3_val1,f3_val2,f3_val3,f3_val4,f3_val5],
    ,[field4,f4_val1,f4_val2]] )

    }
    }




  2. #2
    Paul Clapham Guest

    Re: converting vector to xml

    Better? Do you have any particular problems with it yourself? Just a
    couple of suggestions:

    1. You are repeatedly calculating "<" + key + ">" and its closing relative;
    you could do that once and store it in a String variable.

    2. Your text nodes may contain characters (&, <, >, ', and ") which must be
    escaped (&amp;, &lt;, &gt;, &quot;, and &apos in valid XML. You aren't
    doing that, so you need some code that does these replacements to all text
    nodes (but not element names).

    PC2

    "peter " <pakoo@hotmail.com> wrote in message news:3c7d4add$1@10.1.10.29...
    >
    > Input will be vector whose each element will itself be a vector.Length of
    > vector of each element may vary for different calls.Only the selected

    element
    > needs to be placed in proper order in the output.For ex. field2 is not

    required
    > in the output.Also field4 is placed before field3.Below is the code that
    > I tried to write.Can any one help me with better solution.
    >
    > The Input is vector :
    > "[ [field1,f1_val1,f1_val2,f1_val3,f1_val4]

    ,[field2,f2_val1,f2_val2,f2_val3,f2_val4],
    >
    > ,[field3,f3_val1,f3_val2,f3_val3,f3_val4,f3_val5],
    > ,[field4,f4_val1,f4_val2]] )
    >
    > Expected output is :
    > < FIELD Row=1>
    > <field1>f1_val</field1>
    > <field4>f2_val1</filed4>
    > <field3>f3_val1</field3>
    > </FIELD>
    > < FIELD Row=2>
    > <field1>f1_va2</field1>
    > <field4>f2_val2</filed4>
    > <field3>f3_val2</field3>
    > </FIELD>
    > < FIELD Row=3>
    > <field1>f1_va3</field1>
    > <field3>f3_val3</field3>
    > </FIELD>
    > < FIELD Row=4>
    > <field1>f1_val4</field1>
    > <field3>f3_val4</field3>
    > </FIELD>
    > < FIELD Row=5>
    > <field3>f3_val5</field3>
    > </FIELD>
    >
    > Code:
    >
    >
    > import java.util.*;
    > import java.io.*;
    > public class TEST
    > {
    > public TEST()
    > {
    > }
    > public String getXXX( String fileName, Vector inVect )
    > {
    > StringBuffer row1 = new StringBuffer();
    > StringBuffer row2 = new StringBuffer();
    > StringBuffer row3 = new StringBuffer();
    > StringBuffer row4 = new StringBuffer();
    > StringBuffer row5 = new StringBuffer();
    > StringBuffer allrows = new StringBuffer();
    >
    > try
    > {
    >
    > for( int j = 0; j < inVect.size(); ++j )
    > {
    >
    > Vector innerVect = (Vector)inVect.elementAt( j );
    > String key = (String)innerVect.elementAt( 0 );
    > if(( key.equalsIgnoreCase("field1")))
    > {
    > row1.append( "<" + key + ">");
    >
    > row1.append(innerVect.elementAt(1).toString());
    > row1.append( "</" + key + ">");
    > row2.append( "<" + key + ">");
    > row2.append(innerVect.elementAt(2).toString());
    > row2.append( "</" + key + ">");
    > row3.append( "<" + key + ">");
    > row3.append(innerVect.elementAt(3).toString());
    > row3.append( "</" + key + ">");
    > row4.append( "<" + key + ">");
    > row4.append(innerVect.elementAt(4).toString());
    > row4.append( "</" + key + ">");
    >
    > } //endif drop_ship_addr1
    > else if (( key.equalsIgnoreCase("field4")))
    > {
    > row1.append( "<" + key + ">");
    >
    > row1.append(innerVect.elementAt(1).toString());
    > row1.append( "</" + key + ">");
    > row2.append( "<" + key + ">");
    > row2.append(innerVect.elementAt(2).toString());
    > row2.append( "</" + key + ">");
    >
    >
    > } //endif
    >
    > else if (( key.equalsIgnoreCase("field3")))
    > {
    > row1.append( "<" + key + ">");
    >
    > row1.append(innerVect.elementAt(1).toString());
    > row1.append( "</" + key + ">");
    > row2.append( "<" + key + ">");
    > row2.append(innerVect.elementAt(2).toString());
    > row2.append( "</" + key + ">");
    > row3.append( "<" + key + ">");
    > row3.append(innerVect.elementAt(3).toString());
    > row3.append( "</" + key + ">");
    > row4.append( "<" + key + ">");
    > row4.append(innerVect.elementAt(4).toString());
    > row4.append( "</" + key + ">");
    > row5.append( "<" + key + ">");
    > row5.append (innerVect.elementAt(5).toString());
    > row5.append( "</" + key + ">");
    >
    > } //endif drop_ship_addr3
    >
    >
    > }
    >
    > allrows.append(row1.toString()) ;
    > allrows.append(row2.toString());
    > allrows.append(row3.toString());
    > allrows.append(row4.toString());
    >
    >
    > }
    > catch( Exception e ) {
    > StringWriter sw = new StringWriter();
    > e.printStackTrace(new PrintWriter(sw));
    > String theMessage = sw.toString();
    > System.out.println( theMessage);
    > };
    > System.out.println( allrows.toString());
    > return allrows.toString();
    > }
    > public static void main(String[] args)
    > {
    > TEST pj2 = new TEST();
    >
    > pj2.getXXX( "output.txt",[ [field1,f1_val1,f1_val2,f1_val3,f1_val4]

    ,[field2,f2_val1,f2_val2,f2_val3,f2_val4],
    >
    > ,[field3,f3_val1,f3_val2,f3_val3,f3_val4,f3_val5],
    > ,[field4,f4_val1,f4_val2]] )
    >
    > }
    > }
    >
    >
    >




  3. #3
    MarkN Guest

    Re: converting vector to xml


    If you want to create valid XML you should probably use a parser. Most will
    convert any 'invalid' characters to properly escaped ones automatically.
    Using a parser is probably slower but usually livable.

    Mark

    "Paul Clapham" <pclapham@core-mark.com> wrote:
    >Better? Do you have any particular problems with it yourself? Just a
    >couple of suggestions:
    >
    >1. You are repeatedly calculating "<" + key + ">" and its closing relative;
    >you could do that once and store it in a String variable.
    >
    >2. Your text nodes may contain characters (&, <, >, ', and ") which must

    be
    >escaped (&, <, >, ", and &apos in valid XML. You aren't
    >doing that, so you need some code that does these replacements to all text
    >nodes (but not element names).
    >
    >PC2
    >
    >"peter " <pakoo@hotmail.com> wrote in message news:3c7d4add$1@10.1.10.29...
    >>
    >> Input will be vector whose each element will itself be a vector.Length

    of
    >> vector of each element may vary for different calls.Only the selected

    >element
    >> needs to be placed in proper order in the output.For ex. field2 is not

    >required
    >> in the output.Also field4 is placed before field3.Below is the code that
    >> I tried to write.Can any one help me with better solution.
    >>
    >> The Input is vector :
    >> "[ [field1,f1_val1,f1_val2,f1_val3,f1_val4]

    >,[field2,f2_val1,f2_val2,f2_val3,f2_val4],
    >>
    >> ,[field3,f3_val1,f3_val2,f3_val3,f3_val4,f3_val5],
    >> ,[field4,f4_val1,f4_val2]] )
    >>
    >> Expected output is :
    >> < FIELD Row=1>
    >> <field1>f1_val</field1>
    >> <field4>f2_val1</filed4>
    >> <field3>f3_val1</field3>
    >> </FIELD>
    >> < FIELD Row=2>
    >> <field1>f1_va2</field1>
    >> <field4>f2_val2</filed4>
    >> <field3>f3_val2</field3>
    >> </FIELD>
    >> < FIELD Row=3>
    >> <field1>f1_va3</field1>
    >> <field3>f3_val3</field3>
    >> </FIELD>
    >> < FIELD Row=4>
    >> <field1>f1_val4</field1>
    >> <field3>f3_val4</field3>
    >> </FIELD>
    >> < FIELD Row=5>
    >> <field3>f3_val5</field3>
    >> </FIELD>
    >>
    >> Code:
    >>
    >>
    >> import java.util.*;
    >> import java.io.*;
    >> public class TEST
    >> {
    >> public TEST()
    >> {
    >> }
    >> public String getXXX( String fileName, Vector inVect )
    >> {
    >> StringBuffer row1 = new StringBuffer();
    >> StringBuffer row2 = new StringBuffer();
    >> StringBuffer row3 = new StringBuffer();
    >> StringBuffer row4 = new StringBuffer();
    >> StringBuffer row5 = new StringBuffer();
    >> StringBuffer allrows = new StringBuffer();
    >>
    >> try
    >> {
    >>
    >> for( int j = 0; j < inVect.size(); ++j )
    >> {
    >>
    >> Vector innerVect = (Vector)inVect.elementAt( j );
    >> String key = (String)innerVect.elementAt( 0 );
    >> if(( key.equalsIgnoreCase("field1")))
    >> {
    >> row1.append( "<" + key + ">");
    >>
    >> row1.append(innerVect.elementAt(1).toString());
    >> row1.append( "</" + key + ">");
    >> row2.append( "<" + key + ">");
    >> row2.append(innerVect.elementAt(2).toString());
    >> row2.append( "</" + key + ">");
    >> row3.append( "<" + key + ">");
    >> row3.append(innerVect.elementAt(3).toString());
    >> row3.append( "</" + key + ">");
    >> row4.append( "<" + key + ">");
    >> row4.append(innerVect.elementAt(4).toString());
    >> row4.append( "</" + key + ">");
    >>
    >> } //endif drop_ship_addr1
    >> else if (( key.equalsIgnoreCase("field4")))
    >> {
    >> row1.append( "<" + key + ">");
    >>
    >> row1.append(innerVect.elementAt(1).toString());
    >> row1.append( "</" + key + ">");
    >> row2.append( "<" + key + ">");
    >> row2.append(innerVect.elementAt(2).toString());
    >> row2.append( "</" + key + ">");
    >>
    >>
    >> } //endif
    >>
    >> else if (( key.equalsIgnoreCase("field3")))
    >> {
    >> row1.append( "<" + key + ">");
    >>
    >> row1.append(innerVect.elementAt(1).toString());
    >> row1.append( "</" + key + ">");
    >> row2.append( "<" + key + ">");
    >> row2.append(innerVect.elementAt(2).toString());
    >> row2.append( "</" + key + ">");
    >> row3.append( "<" + key + ">");
    >> row3.append(innerVect.elementAt(3).toString());
    >> row3.append( "</" + key + ">");
    >> row4.append( "<" + key + ">");
    >> row4.append(innerVect.elementAt(4).toString());
    >> row4.append( "</" + key + ">");
    >> row5.append( "<" + key + ">");
    >> row5.append (innerVect.elementAt(5).toString());
    >> row5.append( "</" + key + ">");
    >>
    >> } //endif drop_ship_addr3
    >>
    >>
    >> }
    >>
    >> allrows.append(row1.toString()) ;
    >> allrows.append(row2.toString());
    >> allrows.append(row3.toString());
    >> allrows.append(row4.toString());
    >>
    >>
    >> }
    >> catch( Exception e ) {
    >> StringWriter sw = new StringWriter();
    >> e.printStackTrace(new PrintWriter(sw));
    >> String theMessage = sw.toString();
    >> System.out.println( theMessage);
    >> };
    >> System.out.println( allrows.toString());
    >> return allrows.toString();
    >> }
    >> public static void main(String[] args)
    >> {
    >> TEST pj2 = new TEST();
    >>
    >> pj2.getXXX( "output.txt",[ [field1,f1_val1,f1_val2,f1_val3,f1_val4]

    >,[field2,f2_val1,f2_val2,f2_val3,f2_val4],
    >>
    >> ,[field3,f3_val1,f3_val2,f3_val3,f3_val4,f3_val5],
    >> ,[field4,f4_val1,f4_val2]] )
    >>
    >> }
    >> }
    >>
    >>
    >>

    >
    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center