PBDOM 解析和產生XML

來源:互聯網
上載者:User

因為項目需要,研究了一下pb dom 處理xml 的功能,發現還挺方便的。廢話少說,直接進入正題:

1. PBDOM設定
1)添加pbdom120.pbd(%SYBASE%/Shared/PowerBuilder)到工程的pbl列表中
2) %SYBASE%/Shared/PowerBuilder應該在系統路徑或者應用程式的路徑中(也就是pbdom要使用此路徑下的pbdom120.pbd, PBXerces120.dll,xerces-c_2_6.dll,xerces-depdom_2_6.dll 檔案,同樣,當程式發布時候也需要)

 

2. order.xml

<?xml version="1.0" encoding="UTF-16LE" standalone="no"?>

<order><order_row My_Attr="MyMusic"><order_no My_order="MyorderNo">HKGHKGEQ10072310913</order_no><order_status>EIS</order_status></order_row><order_row><order_no>HKGHKGSP1007237917</order_no><order_status>STD</order_status></order_row><order_row><order_no>HKGHKGEQ10072310915</order_no><order_status>EIS</order_status></order_row><order_row><order_no>HKGHKGEQ10072310916</order_no><order_status>STD</order_status></order_row><order_row><order_no>HKGHKGEQ10072310917</order_no><order_status>STD</order_status></order_row></order>

 

3. 解析order.xml

 

要點:解析xml 無非是取得tag索引值對 和 tag屬性的索引值對。 pb dom很好的提供這個功能。思路都是先把目標放到一個數組,然後遍曆出來,再根據名字得到值。

1) tag索引值對 --〉

root = doc.GetRootElement() --〉先得到根tag
root.GetChildElements(children) --> 得到第二層的tag. 你也可以通過同樣的方法去得到sub tag 的sub tag. 最後遍曆完所有的tag.

 

2) tag屬性的索引值對

 //get all the attributes
 sub_elements[ll_j].GetAttributes(node_attribute)  --> 得到所有的tag屬性名稱

 ls_attribute = node_attribute[2].GetQualifiedName()
 messagebox("order node_attribute",ls_attribute)
 ls_attribute = node_attribute[2].GetName()
 messagebox("order node_attribute",ls_attribute)
 messagebox("order attribute value",node_attribute[2].getText())  -->得到所有的tag屬性名稱

 

3)以下是詳細代碼,不具備通用性,無法處理所有的xml,僅僅是示範如何取xml的值。

程式改進的想法:

3.1)使用遞迴來遍曆所有的節點? 不知道是否可行,還沒有想到如何應用遞迴來實現。因為暫時還沒有想法如何更好地將xml的sub tag的內容再作為一個xml參數 來遞迴調用。

3.2)不過還是可以這樣改進來作為商業用途:根據xml的模版,確定層次。確定類似如下的結構:

order.order_row.order_no   --> order_no

order.order_row.order_status   --> order_status

 

解析xml也是用這樣的索引值對:

order.order_row.order_no   --> HKGHKGEQ10072310913

order.order_row.order_status   --> EIS

 

這樣,最後知道 order_no = HKGHKGEQ10072310913, status = EIS. 從而得到我們所關心的所有資訊,進行後續的處理。

 

 

pbdom_builder builder
pbdom_document doc

pbdom_element root
pbdom_element children[]
pbdom_element node_element
pbdom_element sub_elements[]

pbdom_attribute node_attribute[]

//pbdom_object pbdom_obj_array[]

string ls_text
long ll_i, ll_j
string ls_name
string ls_attribute
string ls_value

builder = create pbdom_builder
doc = builder.BuildFromFile( "d:/order.xml")

// Get the root element of the document
if doc.HasRootElement() then
 root = doc.GetRootElement()
 
 //get the child elements under <order>, in other words, get the total nodes for "<order_row>"
 root.GetChildElements(children)
 
 for ll_i = 1 to UpperBound(children)
  //get the sub node info
  node_element =  children[ll_i]
  
  //get the node name
  //ls_name = node_element.getName() //ok.  return Header for <soapenv:Header>
  ls_name = node_element.GetQualifiedName() //full name, return soapenv:Header for <soapenv:Header>
  messagebox("ls_name",ls_name)
  
  //get the node's attribute
  if node_element.HasAttributes() then
   ls_attribute = node_element.GetAttributeValue("My_Attr") //hard code the arttribute
   if not isnull(ls_attribute) then
    messagebox("ls_attribute",ls_attribute)
   end if
  end if
  
  //get the current node's child elements, in other words, get the order_no, order_status elements.
  node_element.GetChildElements(sub_elements)
  
  //show the sub_elements
  for ll_j = 1 to UpperBound(sub_elements)
   //get the attribute "My_order"
   if sub_elements[ll_j].HasAttributes() then
    //ls_attribute = sub_elements[ll_j].GetAttributeValue("My_order") //ok
    ls_attribute = sub_elements[ll_j].GetQualifiedName()
    messagebox("order ls_attribute",ls_attribute)
    
    ls_attribute = sub_elements[ll_j].getName()
    messagebox("order getName",ls_attribute)
    
    ls_attribute = sub_elements[ll_j].GetNamespacePrefix()
    messagebox("order GetNamespacePrefix",ls_attribute)
    
    //get name space url
    ls_attribute = sub_elements[ll_j].GetNamespaceUri()
    messagebox("order GetNamespaceUri",ls_attribute)
    
    //get all the attributes
    sub_elements[ll_j].GetAttributes(node_attribute)
    ls_attribute = node_attribute[2].GetQualifiedName()
    messagebox("order node_attribute",ls_attribute)
    ls_attribute = node_attribute[2].GetName()
    messagebox("order node_attribute",ls_attribute)
    messagebox("order attribute value",node_attribute[2].getText())

    
   end if
   
   //get the Node: order_row --> order_no, order_status
   //sub_elements[ll_j].GetContent(pbdom_obj_array)
   //ls_name = pbdom_obj_array[1].getName()
   //ls_text = pbdom_obj_array[1].getText()
   
   //get the Node: order_row --> order_no, order_status
   ls_name = sub_elements[ll_j].getName()
   ls_text = sub_elements[ll_j].getText()
   messagebox(ls_name,ls_text)

  next
  
 next
end if

 

 

 4. 產生xml

要點:解析xml 無非是給tag 和 tag屬性賦值

 

TRY

   PBDOM_ELEMENT pbdom_elem_1

   PBDOM_ELEMENT pbdom_elem_2

   PBDOM_ELEMENT pbdom_elem_3
 
   PBDOM_ELEMENT pbdom_elem_4
 
   PBDOM_DOCUMENT pbdom_doc1
 
 pbdom_attribute pbdom_attr1

 string ls_xml

   pbdom_doc1 = Create PBDOM_DOCUMENT

   pbdom_elem_1 = Create PBDOM_ELEMENT

   pbdom_elem_2 = Create PBDOM_ELEMENT

   pbdom_elem_3 = Create PBDOM_ELEMENT

 pbdom_elem_4 = Create PBDOM_ELEMENT
 
 pbdom_attr1 = create pbdom_attribute
 pbdom_attr1.setName("attribute1")

   pbdom_elem_1.SetName("pbdom_elem_1")
//pbdom_elem_1.SetText( "this is the pbdom_elem_1" )
 
//pbdom_elem_1.SetAttribute("attr1:test", "attr1_testing")
pbdom_elem_1.SetAttribute("attr12", "attr1_testing2")

 pbdom_attr1.SetName ("a")
 pbdom_attr1.SetNamespace ("pre2",    "http://www.pre.com", false)
 pbdom_attr1.SetText("456")
 pbdom_elem_1.SetAttribute(pbdom_attr1)

 pbdom_elem_2.SetName("pbdom_elem_2")
 pbdom_elem_2.SetText( "this is the pbdom_elem_2" )

 pbdom_elem_3.SetName("pbdom_elem_3")
//pbdom_elem_3.SetText( "this is the pbdom_elem_3" )
 
 pbdom_elem_4.SetName("pbdom_elem_4")
 pbdom_elem_4.SetText( "this is the pbdom_elem_4" )

 pbdom_elem_3.AddContent(pbdom_elem_4)

 pbdom_elem_1.AddContent(pbdom_elem_2)

 pbdom_elem_1.AddContent(pbdom_elem_3)

 pbdom_doc1.NewDocument("", "", "Root_Element", "", "")
 // pbdom_doc1.NewDocument ("pre", "http://www.pre.com",  "root", "public_id", "system_id.dtd")

 pbdom_doc1.GetRootElement().Detach()

 pbdom_doc1.AddContent(pbdom_elem_1)

ls_xml = pbdom_doc1.SaveDocumentIntoString()
messagebox(ls_xml,ls_xml)

CATCH (pbdom_exception ex)

   MessageBox("Exception", ex.getMessage())

END TRY

 

結果:

<!DOCTYPE Root_Element>
<pbdom_elem_1 attr12="attr1_testing2" pre2:a="456"><pbdom_elem_2>this is the pbdom_elem_2</pbdom_elem_2><pbdom_elem_3><pbdom_elem_4>this is the pbdom_elem_4</pbdom_elem_4></pbdom_elem_3></pbdom_elem_1>

 

5. 以上需要等項目確定了,再做進一步的完善,到時再更新上來。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.