读书人

PB如何操作XML呢

发布时间: 2013-01-01 14:04:18 作者: rapoo

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


table.RemoveAttribute( "cellspacing" )
// Remove all attributes
PBDOM_Attribute empty[]
table.SetAttributes( empty ) // the PowerScript way

4、PBDOM_Text
4.1 操作元素文本内容
<description>
cool demo
</description>

// the text is directly available returns
// "~r~ncool demo~r~n"
ls_desc= elem.GetText()

// two convenience methods
ls_desc= elem.GetTextTrim()// returns "cool demo"
ls_desc = elem.GetTextNormalize()// returns "cool demo"

// text can be changed directly
elem.SetText( "a new description" )

5、PBDOM_Object
5.1 操作有混合内容的元素
<description>
<!comment -->
<?convert units="metric" ?>
cool demo
</description>

PBDOM_Object content[]
desc.GetContent( content )
FOR i = 1 TO UpperBound( content )
CHOOSE content[i].GetObjectClassString()
CASE "pbdom_comment"
// ...
CASE "pbdom_processinginstruction"
// ...
END CHOOSE
NEXT

6、PBDOM_ProcessingInstruction
6.1 使用处理命令(Processing instructions)
<? xml-stylesheet type="text/xsl"href="foo.xsl" _fcksavedurl=""foo.xsl"" ?>
{------target------} {----------------data---------------}


// Get target (e.g., "xsl-stylesheet")
ls_target = pi.GetTarget()

// Get data (e.g., 'type="text/xsl"href="foo.xsl"')
ls_data = pi.GetText()

// Get individual values as attributes
String names[]
pi.GetNames( names )
FOR i = 1 TO UpperBound( names )
MessageBox( names[i], pi.GetValue( names[i] )
NEXT

7、PBDOM and 命名空间(Namespaces)
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="ffsection" select="//SITE_SECTION
<xsl:template name="TopNav">
......
</xsl:template>
</xsl:stylesheet>

String ls_element
PBDOM_Element template


// get element name and namespace return "xsl:template"
template = root.GetChildElement( "template" )
ls_element= template.GetNamespacePrefix() +":"+ template.Getname()

// get element by name and namespace
template = root.GetChildElement( "template", "xsl", "http://www.w3.org/1999/XSL/Transform")


七、PBDOM vs. the Competition
◆Apache Xerces/COM
Xerces 是现在PBDOM底层使用的XML解析器,但对PowerBuiler用户来说使用不直观。(Xerces is the [current] underlying XML parser for PBDOM, but is less intuitive)

◆MSXML
.无法在UNIX等操作系统上使用(No deployment to UNIX possible)
.同样对PowerBuiler用户来说使用不够直观(Less Intuitive)
.COM collections vs. PB arrays (pb用户当然会优先选择使用数组来处理数据)

◆Others
Expata C DLL (使用时需要声明外部函数…使用不够广泛)

------解决方案--------------------


设置个模板IMPORT ok!
[解决办法]
我有一个用户对象(网上下的),是用来解析和输出xml用的,你要是打算要可以留下Email,我发给你。

这个用户对象还是挺好用的,生成xml的时候很快,但是解析大的xml就比较慢。
[解决办法]
像2楼所发的是详细的解析XML;
如果你仅仅是导出导入XML,那么用PB自带的importFile及saleAs,选择类型为XML!就可以了,无需自己写个解析代码!

读书人网 >PB

热点推荐