drmohundro / SWXMLHash

Simple XML parsing in Swift
MIT License
1.41k stars 205 forks source link

check if element contain an attribute #233

Closed Moustafa2050 closed 4 years ago

Moustafa2050 commented 4 years ago

for example i have this element :

if i want to know if element Fee is contain the attribute Amount or not. how can get this?

drmohundro commented 4 years ago

Hi @Moustafa2050 - I'm not sure what happened, but I'm not seeing any code snippets or anything... can you clarify your question?

Moustafa2050 commented 4 years ago

Sorry for that and i don’t know what happened , my question is : if i have this element in XML format : <Fee CurrencyCode="USD" Amount="733.11" Purpose="22"></Fee> how can i check if element " Fee " has an attribute which name as example is " Amount " or not?

drmohundro commented 4 years ago

I'm guessing you have multiple elements that are called Fee but not all have Amount?

Check out this small sample:

let issueXML = """
<Fees>
<Fee CurrencyCode="USD"></Fee>
<Fee CurrencyCode="USD" Amount="733.11" Purpose="22"></Fee>
</Fees>
"""

let issue = SWXMLHash.parse(issueXML)

let found = issue["Fees"]["Fee"].all.filter { idx in idx.element?.attribute(by: "Amount") != nil }

// only prints the one with the Amount attribute
print(found)

Does this help?

Moustafa2050 commented 4 years ago

yes , that all i need,

thank you so much .