Comments (11)
@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 & 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.
@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.
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.
You are really one of the genii of our time
from xmlmapper.
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.
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.
@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.
@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.
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.
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 & 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.
@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)
- Body is located before Header (Unordered tags) HOT 5
- Cannot correctly parse elements with innerText and optional attributes HOT 5
- Parse failing when illegal characters are present HOT 1
- Getting nil for array of elements HOT 2
- Invalid XML string for tags with XML encoded values HOT 1
- Fix swift 5 warnings please. HOT 1
- Alamofire incompatability? HOT 2
- Ordered XML Attributes HOT 3
- Issue with optional attributes in simple tags
- Update to Alamofire 5 HOT 4
- Request subspec on Alamofire 4.9 HOT 2
- foobar2000 returns artist twice - once with just text, second time with attribute - XMLMapper can't access second array item HOT 5
- Alamofire 5.2 cannot convert value of type HOT 9
- Question regarding dots in value HOT 2
- Recursive XML elements HOT 6
- How can I use Requests classes with Swift Package Manager? HOT 1
- Duplicate tag but different data HOT 4
- Use XMLMapper to render the exact nodes order for an XML file as they appear? HOT 6
- Privacy Manifest HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from xmlmapper.