この C++ アプリケーションは、次のような XML ファイルから構成データを読み取ります。
<data>
<value id="FOO1" name="foo1" size="10" description="the foo" ... />
<value id="FOO2" name="foo2" size="10" description="the other foo" ... />
...
<value id="FOO300" name="foo300" size="10" description="the last foo" ... />
</data>
完全なアプリケーション構成は、これらの XML ファイル (150 万を超えるキーと値の属性のペアに変換されます) の約 2500 で構成されます。XML ファイルは、さまざまなソース/チームから取得され、スキーマに対して検証されます。ただし、<value/>
ノードが次のようになる場合があります。
<value name="bar1" id="BAR1" description="the bar" size="20" ... />
またはこれ:
<value id="BAT1" description="the bat" name="bat1" size="25" ... />
このプロセスを高速化するために、Expatを使用して XML ドキュメントを解析しています。Expat は属性を配列として公開します - 次のように:
void ExpatParser::StartElement(const XML_Char* name, const XML_Char** atts)
{
// The attributes are stored in an array of XML_Char* where:
// the nth element is the 'key'
// the n+1 element is the value
// the final element is NULL
for (int i = 0; atts[i]; i += 2)
{
std::string key = atts[i];
std::string value = atts[i + 1];
ProcessAttribute (key, value);
}
}
ProcessAttribute()
これにより、「キー」を読み取り、値をどうするかを決定する すべての責任が関数に置かれます。アプリをプロファイリングすると、合計 XML 解析時間の約 40% が名前/文字列によってこれらの属性を処理していることが示されました。
属性の順序を保証/強制できれば、プロセス全体を劇的に高速化できます (まず、ProcessAttribute()
. たとえば、'id' 属性が常に1 番目の属性である場合、それを直接処理できます。
void ExpatParser::StartElement(const XML_Char* name, const XML_Char** atts)
{
// The attributes are stored in an array of XML_Char* where:
// the nth element is the 'key'
// the n+1 element is the value
// the final element is NULL
ProcessID (atts[1]);
ProcessName (atts[3]);
//etc.
}
W3C スキーマ仕様によると<xs:sequence>
、XML スキーマで要素の順序を強制するために使用できますが、属性に対しては機能しないようです。または、間違って使用している可能性があります。
<xs:element name="data">
<xs:complexType>
<xs:sequence>
<xs:element name="value" type="value_type" minOccurs="1" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="value_type">
<!-- This doesn't work -->
<xs:sequence>
<xs:attribute name="id" type="xs:string" />
<xs:attribute name="name" type="xs:string" />
<xs:attribute name="description" type="xs:string" />
</xs:sequence>
</xs:complexType>
XML ドキュメントで属性の順序を強制する方法はありますか? 答えが「いいえ」の場合、実行時のパフォーマンスに大きなペナルティをもたらさない代替案を誰かが提案できますか?