如何将值节点移动到其父节点的属性?

时间:2009-06-12 14:27:37

标签: python soap suds

我需要更改什么,以便FieldRef下的Name节点是FieldRef的属性,而不是子节点?

Suds目前生成以下肥皂:

<ns0:query>
  <ns0:Where>
    <ns0:Eq>
      <ns0:FieldRef>
        <ns0:Name>_ows_ID</ns0:Name>
      </ns0:FieldRef>
      <ns0:Value>66</ns0:Value>
    </ns0:Eq>
  </ns0:Where>
</ns0:query>

我需要的是:

<ns0:query>
  <ns0:Where>
     <ns0:Eq>
        <ns0:FieldRef Name="_ows_ID">
        </ns0:FieldRef>
        <ns0:Value>66</ns0:Value>
     </ns0:Eq>
  </ns0:Where>
</ns0:query>

第一个xml结构是由以下代码中的suds生成的。

q = c.factory.create('GetListItems.query')
q['Where']=InstFactory.object('Where')
q['Where']['Eq']=InstFactory.object('Eq')
q['Where']['Eq']['FieldRef']=InstFactory.object('FieldRef')
q['Where']['Eq']['FieldRef'].Name='_ows_ID'
q['Where']['Eq']['Value']='66'

print(q)会产生

(query){
   Where = 
      (Where){
         Eq = 
            (Eq){
               FieldRef = 
                  (FieldRef){
                     Name = "_ows_ID"
                  }
               Value = "66"
            }
      }
 }

这是进行创建soap请求的WS调用的代码

c = client.Client(url='https://community.site.edu/_vti_bin/Lists.asmx?WSDL',
                  transport=WindowsHttpAuthenticated(username='domain\user',
                                                     password='password')
                                             )
ll= c.service.GetListItems(listName="{BD59F6D9-AB4B-474D-BCC7-E4B4BEA7EB27}",
                             viewName="{407A6AB9-97CF-4E1F-8544-7DD67CEA997B}",
                             query=q
                             )

1 个答案:

答案 0 :(得分:0)

from suds.sax.element import Element
#create the nodes
q = Element('query')
where=Element('Where')
eq=Element('Eq')
fieldref=Element('FieldRef')
fieldref.set('Name', '_ows_ID')
value=Element('Value')
value.setText('66')

#append them
eq.append(fieldref)
eq.append(value)
where.append(eq)
q.append(where)

https://fedorahosted.org/suds/wiki/TipsAndTricks

  

包含Literal XML

     

包含文字(非转义)XML   作为对象的参数值   属性,您需要设置值   对象的参数   属性为sax元素。该   marshaller的设计简单   附加和附加内容   已经是XML。

     

例如,你想通过   以XML作为参数:

     

<query> <name>Elmer Fudd</name>
  <age unit="years">33</age>
  <job>Wabbit Hunter</job> </query>

     

可以按如下方式完成:

from suds.sax.element import Element
query = Element('query')
name = Element('name').setText('Elmer Fudd')
age = Element('age').setText('33')
age.set('units', 'years')
job = Element('job').setText('Wabbit Hunter')
query.append(name)
query.append(age)
query.append(job)
client.service.runQuery(query)