高效、低成本的XML數(shù)據(jù)綁定和代碼生成XML加快了項目的上市時間
從XML schema生成構(gòu)造良好、可靠類庫的復(fù)雜性可能會挑戰(zhàn)任何項目的時間表和人員生產(chǎn)力。XBinder XSD代碼生成工具根據(jù)XML schema (XSD)文件自動將對象序列化為XML,并將XML反序列化為C、C++、Java或C#對象,從而大大簡化了項目的開發(fā)和維護時間。
XBinder的特性現(xiàn)在包括了:
1. 組在XML Schema 1.1中包含重復(fù)元素和通配符的能力
2. 對于Linux,我們現(xiàn)在提供了用GCC 4、5和6編譯的運行時庫。
3.我們不再為GCC 3構(gòu)建庫。默認(rèn)情況下,示例程序?qū)⑹褂肎CC 4庫。
為什么要使用XML數(shù)據(jù)綁定或XML代碼生成工具?
與傳統(tǒng)XML API(如SAX和DOM)相比,XBinder的XML數(shù)據(jù)綁定具有以下優(yōu)點:
性能——使用XML模式生成的代碼進行驗證或(反)序列化等操作比使用驗證解析器更快。
簡單性——XBinder自動生成構(gòu)造良好、易于閱讀的代碼,從而加快上市周期。
可靠性——XML數(shù)據(jù)綁定應(yīng)用程序通過在模式級別工作來確保生成的XML文檔的有效性。
XBinder的概述
XBinder是C/C++、Java或C#代碼生成工具的XML模式。XML數(shù)據(jù)綁定(或代碼生成)是將XML模式信息項轉(zhuǎn)換為計算機語言中的類型定義和函數(shù)的過程。
XBinder代碼生成工具生成的源代碼是C、C++、Java或C#源代碼,由類型定義和編碼/解碼函數(shù)組成。這為處理XML模式規(guī)范中包含的所有消息定義提供了一個完整的應(yīng)用程序編程接口(API)。
除了代碼生成器之外,程序包中還包含一個公共編碼/解碼函數(shù)的運行時庫。這個庫包含對基本XML模式簡單類型(整數(shù)、字符串、hexBinary等)進行編碼和解碼的例程。XBinder代碼生成工具組裝了一系列對這些函數(shù)的調(diào)用,以完成對更復(fù)雜消息類型的編碼或解碼。
評估版本可用于Windows、Linux、各種UNIX平臺和Apple Mac OSX。
對比
假設(shè)您需要編寫代碼來解析下面的XML實例,并打印出其中包含的所有數(shù)據(jù)。
<purchase>
<customer number="12345">
John Smith
</customer>
<store>
Toys R Us
</store>
<item>
Toy Bath Set
</item>
<price>
19.95
</price>
</purchase>
在這個頁面上,我們將比較在沒有XBinder的情況下解析這個實例所需的代碼量與使用XBinder進行解析所需的代碼量。這兩個例子都將使用C++。對于非XBinder代碼,我們將使用libxml++的DOM功能。下面是不使用XBinder時需要編寫的代碼:
#include <libxml++/libxml++.h>
#include <stdio.h>
int main()
{
// Parse the XML file.
xmlpp::DomParser parser;
parser.set_substitute_entities();
try
{
parser.parse_file("purchase.xml");
}
catch (std::exception& ex)
{
printf("\n%s", ex.what());
}
// Get the root node.
xmlpp::Node* pPurchaseNode = parser.get_document()->get_root_node();
Glib::ustring nodename = pPurchaseNode->get_name();
// Get the root node's children in a list.
xmlpp::Node::NodeList purchaseChildren = pPurchaseNode->get_children();
// Now walk through the children and process them according to what element
// is represented.
for (xmlpp::Node::NodeList::iterator iter = purchaseChildren.begin();
iter != purchaseChildren.end(); ++iter)
{
xmlpp::Node* pChildNode = *iter;
nodename = pChildNode->get_name();
if (nodename == "text")
{
// We'll get the text value for each element explicitly
// as we encounter them.
continue;
}
else if (nodename == "customer")
{
// We're at the <customer> node. We want to print the customer
// name and number.
xmlpp::Element* pChildElement =
dynamic_cast<xmlpp::Element*> (pChildNode);
xmlpp::Attribute* pCustomerAttr =
pChildElement->get_attribute("number");
printf("\nCustomer number: %s", pCustomerAttr->get_value().c_str());
xmlpp::TextNode* pChildText = pChildElement->get_child_text();
printf("\nCustomer name: %s", pChildText->get_content().c_str());
}
else if (nodename == "store")
{
// We're at the <store> node. We want to print the store name.
xmlpp::Element* pChildElement =
dynamic_cast<xmlpp::Element*> (pChildNode);
xmlpp::TextNode* pChildText = pChildElement->get_child_text();
printf("\nStore name: %s", pChildText->get_content().c_str());
}
else if (nodename == "item")
{
// We're at the <item> node. We want to print the item name.
xmlpp::Element* pChildElement =
dynamic_cast<xmlpp::Element*> (pChildNode);
xmlpp::TextNode* pChildText = pChildElement->get_child_text();
printf("\nItem name: %s", pChildText->get_content().c_str());
}
else if (nodename == "price")
{
// We're at the <price> node. We want to print the price.
xmlpp::Element* pChildElement =
dynamic_cast<xmlpp::Element*> (pChildNode);
xmlpp::TextNode* pChildText = pChildElement->get_child_text();
printf("\nPrice: %s", pChildText->get_content().c_str());
}
}
return 0;
}
下面是需要使用XBinder編寫的代碼:
#include "rtxsrc/OSRTFileInputStream.h"
#include "rtxmlsrc/rtXmlCppMsgBuf.h"
#include <Purchase.h>
#include <stdio.h>
int main()
{
// Setup to decode the instance in purchase.xml
int stat;
const char* filename = "Purchase.xml";
OSRTFileInputStream in (filename);
OSXMLDecodeBuffer decodeBuffer (in);
purchase_CC pdu (decodeBuffer);
// Do the decode
stat = pdu.decode();
// Print the information that was in the instance.
PurchaseRecord* pPurchase = pdu.getValue();
printf("\nCustomer number: %d", pPurchase->customer.number);
printf("\nCustomer name: %s", pPurchase->customer.value.c_str());
printf("\nStore name: %s", pPurchase->store.c_str());
PurchaseRecord_3* pItemAndPrice = pPurchase->_seq3.getItem(0);
unsigned short i = 0;
while (pItemAndPrice != 0)
{
printf("\nItem name: %s", pItemAndPrice->item.c_str());
printf("\nItem price: %.2f", pItemAndPrice->price);
pItemAndPrice = pPurchase->_seq3.getItem(++i);
}
return stat;
}
關(guān)于這些代碼示例,有幾點值得注意:
京ICP備09015132號-996 | 違法和不良信息舉報電話:4006561155
© Copyright 2000-2026 北京哲想軟件有限公司版權(quán)所有 | 地址:北京市海淀區(qū)西三環(huán)北路50號豪柏大廈C2座11層1105室
北京哲想軟件集團旗下網(wǎng)站:哲想軟件 | 哲想動畫