簡體   English   中英

如何解析XML字符串中的第一個屬性?

[英]How to parse the first attribute in an XML string?

您能幫我這個程序嗎,我正在嘗試解析一個xml文件,我的XML看起來像:這是我的XML

<pre>
<site>
<restaurant capacite="450" heure_debut_service="11:30:00" heure_fin_service="14:00:00" nom="LE MARCHE">
        <disponibilite>
            <d i="haute" o="0" t="0"/>
            <d i="haute" o="-17" t="10"/>
            <d i="haute" o="-13" t="20"/>
            <d i="haute" o="42" t="30"/>
            <d i="haute" o="109" t="40"/>
            <d i="haute" o="222" t="50"/>
            <d i="complet" o="298" t="60"/>
            <d i="complet" o="323" t="70"/>
            <d i="complet" o="315" t="80"/>
            <d i="complet" o="302" t="90"/>
            <d i="faible" o="285" t="100"/>
            <d i="faible" o="255" t="110"/>
            <d i="haute" o="213" t="120"/>
            <d i="haute" o="152" t="130"/>
            <d i="haute" o="96" t="140"/>
            <d i="haute" o="49" t="150"/>
        </disponibilite>
        <fluidite>
            <f i="fluide" o="0" t="0"/> // I want to search this value i="fluide"
            <f i="fluide" o="13" t="10"/>
            <f i="fluide" o="25" t="20"/>
            <f i="fluide" o="35" t="30"/>
            <f i="fluide" o="84" t="40"/>
            <f i="fluide" o="80" t="50"/>
            <f i="fluide" o="67" t="60"/>
            <f i="fluide" o="66" t="70"/>
            <f i="fluide" o="61" t="80"/>
            <f i="fluide" o="56" t="90"/>
            <f i="fluide" o="48" t="100"/>
            <f i="fluide" o="34" t="110"/>
            <f i="fluide" o="20" t="120"/>
            <f i="fluide" o="13" t="130"/>
            <f i="fluide" o="9" t="140"/>
            <f i="12" o="3" t="150"/>
        </fluidite>
        <stand nom="PIZZA PASTA ETC.">
            <fluidite>
                <f i="pertubé" t="0"/>
                <f i="fluide" t="10"/>
                <f i="fluide" t="20"/>
                <f i="fluide" t="30"/>
                <f i="ralenti" t="40"/>
                <f i="ralenti" t="50"/>
                <f i="fluide" t="60"/>
                <f i="ralenti" t="70"/>
                <f i="fluide" t="80"/>
                <f i="fluide" t="90"/>
                <f i="fluide" t="100"/>
                <f i="fluide" t="110"/>
                <f i="fluide" t="120"/>
                <f i="fluide" t="130"/>
                <f i="fluide" t="140"/>
                <f i="ralenti" t="150"/>
            </fluidite>
            <menuSemaine>
                <menu jour="0">
                    <plat>
                        Kebab   Sauce yaourt
                    </plat>
                </menu>
            </menuSemaine>
        </stand>
        <stand nom="COCOTTES">
            <fluidite>
                <f i="fluide" t="0"/> //My code search this value "i=fluide"
                <f i="fluide" t="10"/>
                <f i="fluide" t="20"/>
                <f i="fluide" t="30"/>
                <f i="ralenti" t="40"/>
                <f i="ralenti" t="50"/>
                <f i="fluide" t="60"/>
                <f i="ralenti" t="70"/>
                <f i="fluide" t="80"/>
                <f i="fluide" t="90"/>
                <f i="fluide" t="100"/>
                <f i="fluide" t="110"/>
                <f i="fluide" t="120"/>
                <f i="fluide" t="130"/>
                <f i="fluide" t="140"/>
                <f i="fluide" t="150"/>
            </fluidite>
            <menuSemaine>
                <menu jour="0">
                    <plat>
                        Escalope de dinde, crème forestière, macaronis
                    </plat>
                </menu>
            </menuSemaine>
        </stand>
</site>     
</pre>

我的解析器代碼如下:

  • (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {

    self.currentElement = elementName;

    if ([elementName isEqualToString:@"restaurant"]) {

    self.aArticle = [[Article alloc]init];
    self.aArticle.key = [NSString stringWithFormat:@"art%d",self.counter];
    self.counter++;
    

    }

    //Fluidite if ([elementName isEqualToString:@"f"]) { self.aArticle.datePublished2 = [attributeDict valueForKey:@"t"]; if ([self.aArticle.datePublished2 isEqualToString:@"0"]) { self.aArticle.datePublished = [attributeDict valueForKey:@"i"]; } }

}

我的代碼搜索此值“ i = fluide”。 站立-> nom =“ COCOTTES”->流質-> fi =“ fluide”

但是我希望這個價值立場->餐館->輕石-> f-> i =“ fluide”(第一行任何幫助將不勝感激......

謝謝

看看這個類在GitHub上,因為它會為你節省大量的時間。 我在許多不同的項目中使用了它。 您最可能感興趣的方法是+ (NSDictionary *)dictionaryWithXMLFile:(NSString *)path; + (NSDictionary *)dictionaryWithXMLData:(NSData *)data;

您將得到一個嵌套的NSDictionary對象,可以使用標准鍵/值操作或使用如下所示的塊來遍歷該對象

[dictionary enumerateKeysAndObjectsWithOptions:NSEnumerationConcurrent
                          usingBlock:^(id key, id object, BOOL *stop) {

}];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM