Code Monkey home page Code Monkey logo

Comments (11)

gcharita avatar gcharita commented on June 12, 2024 3

@meshileya the & character is not allowed as is in an XML. This should be & in order to parsed correctly by the system. (you can find more info on XML allowed characters in this link)

Having said that, if your XML looks like this:

<root>
    <itunes:category text="Life &amp; Love"/>
</root>

You can use nested attribute mapping to map text attribute with the following model:

class Root: XMLMappable {
    var nodeName: String!
    
    var itunesCategoryText: String?
    
    required init?(map: XMLMap) {}
    
    func mapping(map: XMLMap) {
        itunesCategoryText <- map.attributes["itunes:category.text"]
    }
}

Hope this helps.

from xmlmapper.

gcharita avatar gcharita commented on June 12, 2024 2

@meshileya thank you for choosing this library. 😃

To use NSKeyedArchiver your RSSFeeds type must implement NSCoding protocol (you can find an example on how to do that in this link)

If you don't want to use NSCoding protocol (given the fact that your RSSFeeds type implements XMLMappable protocol) you can try XMLSerialization to convert your XML into data like:

func saveListXMLData(podList: [RSSFeeds]) {
    let podData = try? XMLSerialization.data(withXMLObject: podList.toXML())
    UserDefaults.standard.set(podData, forKey: "podData")
    UserDefaults.standard.synchronize()
}

Just remember to convert the data back to your type, when you are retrieving them like:

func getListXMLData() -> [RSSFeeds]? {
    guard let data = UserDefaults.standard.data(forKey: "podData") else {
        return nil
    }
    let xml = try? XMLSerialization.xmlObject(with: data)
    return XMLMapper<RSSFeeds>().mapArray(XMLObject: xml)
}

On the other hand, you can just save your XML string to the UserDefaults like:

func saveListXMLString(podList: [RSSFeeds]) {
    UserDefaults.standard.set(podList.toXMLString(), forKey: "podData")
    UserDefaults.standard.synchronize()
}

And map it back to your object type, when you are retrieving it like:

func getListXMLString() -> [RSSFeeds]? {
    guard let xmlString = UserDefaults.standard.string(forKey: "podData") else {
        return nil
    }
    return XMLMapper<RSSFeeds>().mapArray(XMLString: xmlString)
}

All of the above ways will have the same outcome.

Hope this helps.

from xmlmapper.

meshileya avatar meshileya commented on June 12, 2024 1

@gcharita

This is actually the way I am doing the write and read

 func savePod(podList: [RSSFeeds]) {
        UserDefaults.standard.set(podList.toXMLString(), forKey: "podData")
        UserDefaults.standard.synchronize()
    }
    
    func loadPod() -> [RSSFeeds]? {
        guard let xmlString = UserDefaults.standard.string(forKey: "podData") else {
            return nil
        }
        return XMLMapper<RSSFeed>().mapArray(XMLString: xmlString)
    }

It only happens when I have itunesCategoryText <- map.attributes["itunes:category.text"]

from xmlmapper.

meshileya avatar meshileya commented on June 12, 2024 1

You are really one of the genii of our time

from xmlmapper.

meshileya avatar meshileya commented on June 12, 2024

Thanks so much @gcharita , I really do appreciate this detailed response.

Just for clarity sake, I will like to know if there is a special way I can get the text of this xml.

<itunes:category text="Life & Love"/>

Thanks once again, I really do appreciate.
@gcharita

from xmlmapper.

meshileya avatar meshileya commented on June 12, 2024

Oh yeah, thanks @gcharita I really appreciate.

But I think I have got into a slight issue now.

Whenever I try to save the data using UserDefaults, it actually saves well but if I want to retrieve the data, the below is the error I do get.

The only weird part is, it actually works without having to save in UserDefaults but once that is done, then I get the error below.

I really appreciate your effort bro :(

Error Domain=NSXMLParserErrorDomain Code=68 "(null)" UserInfo={NSXMLParserErrorColumn=56, NSXMLParserErrorLineNumber=1, NSXMLParserErrorMessage=xmlParseEntityRef: no name
}

from xmlmapper.

gcharita avatar gcharita commented on June 12, 2024

@meshileya what method do you use to save to the UserDefaults? If you convert your XML to Data, try using the raw string method.

from xmlmapper.

gcharita avatar gcharita commented on June 12, 2024

@meshileya there is an interesting bug that I just figured it out. Sorry for the inconvenience. Take a look at #23 for a temporary fix.

from xmlmapper.

gcharita avatar gcharita commented on June 12, 2024

I am closing this, since the question was answered and the #23 is not related to this. @meshileya thank you for asking.

For questions regarding XMLMapper feel free to use Stack Overflow.

from xmlmapper.

meshileya avatar meshileya commented on June 12, 2024

Hello @gcharita , I will so much appreciate your insight as regards this.

I am currently working on an XML with several categories. e.g.

<itunes:category text="Society &amp; Culture"/>
        <itunes:category text="Comedy"/>
        <itunes:category text="Arts"/>

but, whenever I use category <- map.attributes["itunes:category.text"] , it returns null.

but it works perfectly well for XML with just one itunes:category.

I will like to ask if there is anything I am doing wrong, pls.

from xmlmapper.

gcharita avatar gcharita commented on June 12, 2024

@meshileya unfortunately, you have to create a new XMLMappable class-struct in order to map that XML. So you can try something like this:

class Root: XMLMappable {
    var nodeName: String!

    var itunesCategories: [ITunesCategory]?
    
    required init?(map: XMLMap) {}
    
    func mapping(map: XMLMap) {
        itunesCategories <- map["itunes:category"]
    }
}

class ITunesCategory: XMLMappable {
    var nodeName: String!
    
    var text: String?
    
    required init?(map: XMLMap) {}
    
    func mapping(map: XMLMap) {
        text <- map.attributes["text"]
    }
}

Don't worry if you receive only one itunes:category tag. XMLMapper will handle that and generate an Array with only one element 😉

from xmlmapper.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.