Home > プログラム > libxml2でattributeを取りたい

libxml2でattributeを取りたい

saxパーサであるlibxml2でattributeの中身を出すのに苦労したので、ここに書きます。

xmlSAXHandlerの「startElementNsSAX2Func」部分に、読み出してくれる関数を設定し、
受け側関数部分は、
static void startElementSAX(void *ctx, const xmlChar *localname, const xmlChar *prefix, const xmlChar *URI, int nb_namespaces, const xmlChar **namespaces, int nb_attributes, int nb_defaulted, const xmlChar **attributes)
の様に書きますが、一見、attributesをnb_attributesの数だけ出力すればすぐ出るんじゃないの?
と思いますが、そこを出しても、例えば

<rss version=”2.0″>

の様なタグがああった場合、「version」という部分しか出ないんですね..。

javaのlibxml2ばかり出てきてしまい、非常に時間がかかったのですが、いろいろ探しまわった結果、

http://blogs.dion.ne.jp/p50p100/archives/6751189.html

に書かれている事をする事で、ちゃんと「2.0」部分まで取れるようになりました。

修正したのは、受側関数部分のattributeの所を以下のように修正、
static void startElementSAX(void *ctx, const xmlChar *localname, const xmlChar *prefix, const xmlChar *URI, int nb_namespaces, const xmlChar **namespaces, int nb_attributes, int nb_defaulted, const xmlChar *(*attributes)[5]) {

それで以下のように

for(int i=0;i  NSString *string = [NSString stringWithFormat:@"%s=\"%.*s\"" , attributes[i][0], attributes[i][4] – attributes[i][3], attributes[i][3] ];
 NSLog(string);
}

取得する事で、きちんと
「version=”2.0″」と取る事が出来ました。

引数の受け取り方自体を書き換え、文字列を切って表示するという方法?でしょうか。

kuni プログラム

  1. dantsu
    7月 28th, 2009 at 00:22 | #1

    私もrefさんと同じところで困っています。

    static void startElementHandler(
    void* ctx,
    const xmlChar *localname,
    const xmlChar *prefix,
    const xmlChar *URI,
    int nb_namespaces,
    const xmlChar **namespaces,
    int nb_attributes,
    int nb_defaulted,
    //const xmlChar** attributes)
    const xmlChar *(*attributes)[5])
    {
    [(ListOperation *)ctx
    startElementLocalName:localname
    prefix:prefix URI:URI
    nb_namespaces:nb_namespaces
    namespaces:namespaces
    nb_attributes:nb_attributes
    nb_defaulted:nb_defaulted
    attributes:attributes];
    }

    ↑のところではなく↓の所のことです

    - (void)startElementLocalName:(const xmlChar *)localname
    prefix:(const xmlChar *)prefix
    URI:(const xmlChar *)URI
    nb_namespaces:(int)nb_namespaces
    namespaces:(const xmlChar **)namespaces
    nb_attributes:(int)nb_attributes
    nb_defaulted:(int)nb_defaulted
    attributes:(const xmlChar **)attributes
    {…}

  2. kuni
    7月 3rd, 2009 at 13:06 | #2

    @ref
    お返事が大変遅くなってしまい、申し訳ないです、
    自分のコードでは、
    static void startElementSAX(void *ctx, const xmlChar *localname, const xmlChar *prefix, const xmlChar *URI,
    int nb_namespaces, const xmlChar **namespaces, int nb_attributes, int nb_defaulted, const xmlChar *(*attributes)[5]);
    の様になっています。参考になりますでしょうか?
    もしまた何かあれば、ご連絡いただければと思います。
    どうぞよろしくお願いいたします。

  3. ref
    6月 30th, 2009 at 02:02 | #3

    同じところでハマってたどり着きました。
    Objective-Cで利用されているようなのですが、startElementSAXの委譲先メソッドの定義はどのようにされていますか?

    - (void)startElementLocalName:(const xmlChar *)localname
    prefix:(const xmlChar *)prefix
    URI:(const xmlChar *)URI
    nb_namespaces:(int)nb_namespaces
    namespaces:(const xmlChar **)namespaces
    nb_attributes:(int)nb_attributes
    nb_defaulted:(int)nb_defaulted
    attributes:(const xmlChar **)attributes

    この定義でattributes[0][0]を参照すると落ちてしまいます
    対処法をもしご存知なら教えていただけないでしょうか。

  4. 五十歩百歩
    6月 6th, 2009 at 20:05 | #4

    コメントとトラバありがとうございます。

    直リンOKですよ。保存期間が過ぎれば切れますが。。。
    http://blogs.dion.ne.jp/p50p100/archives/6751189.html

    複数オーサーって珍しいですね。

  5. 五十歩百歩
    6月 6th, 2009 at 09:55 | #5

    コメント・トラバありがとう。

    直リンOKです。保存期限が過ぎれば切れますが。。。
    http://blogs.dion.ne.jp/p50p100/archives/6751189.html

    複数オーサーって珍しいですね。

  1. No trackbacks yet.