2

2 つの異なるサーバーから送信された xml 応答を解析するクライアント アプリケーションがあります。私はそれらをサーバーAとサーバーBと呼んでいます。

サーバー A は、次のような応答で要求の 1 つに応答します。

<?xml version="1.0" encoding="UTF-8"?>
    <D:multistatus xmlns:D="DAV:">
    <D:response>
    <D:href>/T12.txt</D:href>
    <D:propstat>
    <D:prop>
    <local-modification-time xmlns="urn:abc.com:webdrive">1389692809</local-modification-time>
    </D:prop>

    <D:status>HTTP/1.1 200 OK</D:status>

    </D:propstat>
</D:response>
</D:multistatus>

サーバー B は、次のような応答で要求の 1 つに応答します。

<?xml version="1.0" encoding="UTF-8"?>
    <D:multistatus xmlns:D="DAV:">
    <D:response>
    <D:href>/T12.txt</D:href>
    <D:propstat>
    <D:prop>
    <O:local-modification-time xmlns:O="urn:abc.com:webdrive">1389692809</O:local-modification-time>
    </D:prop>

    <D:status>HTTP/1.1 200 OK</D:status>

    </D:propstat>
</D:response>
</D:multistatus>

2 つのサーバーの違いを観察すると、serverA は名前空間とプレフィックスの間のマッピングを送信しませんが、serverB は送信します (local-modification-time タグを見てください)。これらの両方のシナリオを一般的に処理するために、一般的なクライアント解析ロジックを作成するにはどうすればよいですか。サンプルコードは非常に役立ちます。

ありがとう、 -サンディープ

4

1 に答える 1

3

あなたの最善の策は、名前空間を無視してローカル名でノードを見つけることだと思います。次のように XPath を使用して実行できます。

node.select_single_node(".[local-name()='local-modification-time']")

または、次のように C++ を使用することもできます。

pugi::xml_node child_by_local_name(pugi::xml_node node, const char* name)
{
    for (pugi::xml_node child = node.first_child(); child; child = child.next_sibling())
    {
        if (strcmp(child.name(), name) == 0)
            return child;

        const char* colon = strchr(child.name(), ':');

        if (colon && strcmp(colon + 1, name) == 0)
            return child;
    }

    return pugi::xml_node();
}
于 2014-01-16T16:09:00.603 に答える