使用PBDOM操作XML

使用PBDOM读取XML的一个例子 .
2007-07-28 01:26 570人阅读 评论(0) 收藏 举报

1、创建pbdom_doc_1.xml,内容如下:

]>


Child Data Text

&text;





2、将PBDOM90.PBD添加进来(因为编译器的版本为pb9)

读取代码如下:

PBDOM_BUILDER pbdom_bldr
PBDOM_Document pbdom_doc
PBDOM_Object pbdom_obj_array[]
PBDOM_Element pbdom_elem
integer iFileNum1
long l = 0
Try
// Create a PBDOM_DOCUMENT from the XML file
pbdom_bldr = Create PBDOM_Builder
pbdom_doc = pbdom_bldr.BuildFromFile ("pbdom_doc_1.xml")
// Test the contents of the PBDOM_DOCUMENT
// First test the PBDOM_DOCTYPE in the document
MessageBox ("PBDOM_DOCTYPE GetName()", pbdom_doc.GetDocType().GetName())
MessageBox ("PBDOM_DOCTYPE GetInternalSubset()", pbdom_doc.GetDocType().GetInternalSubset())
// Test the root element
MessageBox ("PBDOM_DOC Root Element Name", pbdom_doc.GetRootElement().GetName())
// test the root element's child element
MessageBox ("PBDOM_DOC Element Name", pbdom_doc.GetRootElement().GetChildElement("data").GetName())
// Collect all the child PBDOM_OBJECTs of the
// element
pbdom_doc.GetRootElement().GetChildElement("data").GetContent(pbdom_obj_array)
// Display the class name, the name and the text contained
// within each PBDOM_OBJECT array item
for l = 1 to UpperBound(pbdom_obj_array)
MessageBox ("Child Object " + string(l) + " Class",pbdom_obj_array[l].GetObjectClassString())
MessageBox ("Child Object " + string(l) + " Name", pbdom_obj_array[l].GetName())
MessageBox ("Child Object " + string(l) + " Text",pbdom_obj_array[l].GetText())
next
// Retrieve and display the name and text value of the
// "An_Attribute" attribute from the element
pbdom_elem = pbdom_obj_array[2]
MessageBox ("child_data Attribute name", pbdom_elem.GetAttribute("An_Attribute").GetName())
MessageBox ("child_data Attribute value", pbdom_elem.GetAttribute("An_Attribute").GetText())
// save the DOM Tree contained within pbdom_doc into
// a separate file "c:/pbdom_doc_2.xml"
pbdom_doc.SaveDocument ("pbdom_doc_2.xml")
Destroy pbdom_bldr
CATCH (PBDOM_Exception except)
MessageBox ("Exception Occurred", except.Text)
END TRY

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////

////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////



因为项目需要,研究了一下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



HKGHKGEQ10072310913EISHKGHKGSP1007237917STDHKGHKGEQ10072310915EISHKGHKGEQ10072310916STDHKGHKGEQ10072310917STD



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 = HKGHKGEQ1007231

0913, 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 , in other words, get the total nodes for ""
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
ls_name = node_element.GetQualifiedName() //full name, return soapenv:Header for
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", "https://www.360docs.net/doc/818638672.html,", 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", "https://www.360docs.net/doc/818638672.html,", "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



结果:


this is the pbdom_elem_2this is the pbdom_elem_4



5. 以上需要等项目确定了,再做进一步的完善,到时再更新上来。





相关文档
最新文档