Coder Social home page Coder Social logo

swiftxml's Introduction

SwiftXML: Simple XML parsing and serializing for Swift.

Import an XML document

Import an XML document simply, and the XML structure will be converted to XMLNode objects.

guard let url = Bundle.main.url(forResource: "books", withExtension: "xml") else { return }
guard let xml = XML(contentsOf: url) else { return }

Use subscripts

Use subscripts to set nodes and attributes:

xml[0][0]["title"]?.text = "Great Expectations"
xml[0][0].attributes["isbn"] = "38"

Similarly, use subscripts to get nodes and properties.

guard let title = xml[0][0]["title"]?.text,
let isbn = xml[0][0].attributes["isbn"] else {return}
print("\(title) with isbn of \(isbn)")

Create an XML node

Instantiate a node with its name, or its name and value:

let bookNode = XMLNode(name:"book")

Add child node

You can add a child node with its name and value:

bookNode.addChild(name: "title", value: "Robinson Crusoe")

Or with another XML node:

xml[0].addChild(bookNode)

Remove child node

You can remove a child node with its index:

xml[0].removeChild(at: 1)

Serialize XML

Regenerate XML structure from your data with the XMLNode's description property:

//either:
print(xml.description)
//or simply:
print(xml)

Install SwiftXML

Drag the XML.swift file to your project

Compatibility

SwiftXML is Swift 4.0 compatible.

swiftxml's People

Contributors

craiggrummitt avatar seeppp avatar

Watchers

Francisco Javier Castañeda Ramos avatar

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.