当前位置: 代码迷 >> PB >> PB如何操作XML呢
  详细解决方案

PB如何操作XML呢

热度:68   发布时间:2016-04-29 07:39:50.0
PB怎么操作XML呢?
给我点提示吧,网上没找到资料,PB怎么读取,怎么写入,一个XML文件,谢谢
------解决方案--------------------
这是我收藏的内容

五、使用PBDOM初步
 ◆PBDOM设置
1、添加pbdom90.pbd(%SYBASE%\Shared\PowerBuilder)到工程的pbl列表中
2、%SYBASE%\Shared\PowerBuilder应该在系统路径或者应用程序的路径中(也就是pbdom要使用此路径下的pbdom90.dll, pbxerces90.dll、xerces_2_1_0.dll文件,同样,当程序发布时候也需要)

六、PBDOM类的使用

 ◆如图所示,反映了PBDOM类的组成和继承关系,可以看到,几乎所有的PBDOM类都继承自PBDOM_Object(除了PBDOM_Builder和PBDOM_Exception)

1、PBDOM_Document
◆构建PBDOM举例

1.1 直接构建(XML documents can be created from scratch)
PBDOM_Document doc
PBDOM_Element rootdoc = CREATE PBDOM_Document
root = CREATE PBDOM_Element
root.SetName( "root" )
root.SetText( "this is the root" )
doc.AddContent( root )

1.2 从文件、字符串、DataStore中载入
PBDOM_Builder builder
doc = builder.BuildFromString( "<foo>bar</foo>" )
doc = builder.BuildFromFile( "c:\foo\bar.xml"
doc = builder.BuildFromDataStore( l_ds)

 

2、PBDOM_Element
2.1 遍历元素
PBDOM_Element root, children[], first
// Get the root element of the document
root = doc.GetRootElement()
// Get an array of all child elements
root.GetChildElements( children )
// Get only elements with a given name
root.GetChildElements( "name", children )
// Get the first element with a given name
first = root.GetChildElement( "name" )

注意:

上例中得到的元素数组是联动的!(The element array is live!) 即:
◆ 修改数组中的元素,同样会作用到父文档
◆ 返回的数组是有界的(Once the array is returned, it is now bounded)
◆ 在数组中增加新元素时,需要一个SetContent()方法调用

2.2 移动元素
// PBDOM_Document docOne,docTwo
PBDOM_Element movable

movable = CREATE PBDOM_Element
Movable.SetName( "movable" )
docOne.AddContent( movable ) // add
movable.Detach() // remove
docTwo.addContent( movable ) // add again


注意:

1、只要是从PBDOM_Object继承的对象,都可以调用Detach()方法(如Comments、ProcessingInstructions、Elements (and their content)等等)
2、PBDOM元素对象不是永久的捆绑在它的父文档上的(PBDOM elements aren't permanently tied to their parent document)

2.3 符合规格(Always well-formed)
PBDOM_Element构造器以及setter方法会检查元素是否符合规格:

       elem.SetName( "Spaces are illegal" )



AddContent()方法也会从以下几个方面进行检查:
◆ 结构---树中没有循环(Structure –no loops in any tree)
◆ 只有一个根节点元素(One and only one root element)
◆ 相容的命名空间(Consistent namespaces)



3、PBDOM_Attribute

3.1  操作元素属性
◆ 元素可以有多个属性

<table width="100%" border="0"></table>


// Get an attribute
ls_width = table.GetAttributeValue( "width" ) // or
ls_width = table.GetAttribute ( "width" ).GetText()
// Attributes can be typed
li_border = table.GetAttribute( "width" ).GetIntValue()

// Set an attribute
table.SetAttribute( "cellspacing", "0" )
// Remove an attribute
  相关解决方案