sponsored links

Collapse

Announcement

Collapse
No announcement yet.

Generating XML With Mix of Tags and Attributes

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Generating XML With Mix of Tags and Attributes


    How do you generate xml from sql when the root tag has attributes, and there is a mixture of elements with values and elements with attributes?

    Example:

    PHP Code:
    <myRequest someAttr="somevalue">
      <
    element1>value</element1>
      <
    element2 attr1="value" attr2="value" />
      <
    element3>value</element3>
    </
    myRequest


  • #2
    Like this:
    Code:
    drop table qtemp/myData;
    create table qtemp/myData (
    elem1     varchar(20),
    elem2     varchar(20),
    elem2a1   varchar(20),
    elem2a2   varchar(20),
    elem3     varchar(20),
    someAttr0 varchar(20));
    
    insert into qtemp/myData
    values('value','','value','value','value','someValue');
    
    select xmlElement(name "myRequest", xmlattributes(someAttr0 as "someAttr"),
             xmlElement(name "element1", elem1),
             xmlElement(name "element2", xmlattributes(elem2a1 as "attr1", elem2a2 as "attr2"), elem2),
             xmlElement(name "element3", elem3)
           )
      from qtemp/myData;
    Output (after I formatted it):
    Code:
    <myRequest someAttr="someValue">
      <element1>value</element1>
      <element2 attr1="value" attr2="value"></element2>
      <element3>value</element3>
    </myRequest>

    Comment


    • #3
      Originally posted by Vectorspace View Post
      Like this:
      Code:
      drop table qtemp/myData;
      create table qtemp/myData (
      elem1 varchar(20),
      elem2 varchar(20),
      elem2a1 varchar(20),
      elem2a2 varchar(20),
      elem3 varchar(20),
      someAttr0 varchar(20));
      
      insert into qtemp/myData
      values('value','','value','value','value','someValue');
      
      select xmlElement(name "myRequest", xmlattributes(someAttr0 as "someAttr"),
      xmlElement(name "element1", elem1),
      xmlElement(name "element2", xmlattributes(elem2a1 as "attr1", elem2a2 as "attr2"), elem2),
      xmlElement(name "element3", elem3)
      )
      from qtemp/myData;
      Output (after I formatted it):
      Code:
      <myRequest someAttr="someValue">
      <element1>value</element1>
      <element2 attr1="value" attr2="value"></element2>
      <element3>value</element3>
      </myRequest>
      That was almost too simple! Thanks! IBM should pay you for your documentation style (or maybe i need to brush up on my google search skills because I couldn't find this example anywhere)

      Comment

      Working...
      X