{"id":19260678,"url":"https://github.com/evant/parsnip","last_synced_at":"2025-04-21T16:32:08.391Z","repository":{"id":57736728,"uuid":"41316825","full_name":"evant/parsnip","owner":"evant","description":" A modern XML library for Android and Java","archived":false,"fork":false,"pushed_at":"2020-06-14T22:50:25.000Z","size":301,"stargazers_count":15,"open_issues_count":5,"forks_count":3,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-01T14:38:38.963Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/evant.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-08-24T17:20:30.000Z","updated_at":"2018-11-16T03:46:57.000Z","dependencies_parsed_at":"2022-08-24T14:57:22.525Z","dependency_job_id":null,"html_url":"https://github.com/evant/parsnip","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evant%2Fparsnip","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evant%2Fparsnip/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evant%2Fparsnip/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evant%2Fparsnip/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/evant","download_url":"https://codeload.github.com/evant/parsnip/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250091017,"owners_count":21373302,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-09T19:22:28.254Z","updated_at":"2025-04-21T16:32:07.698Z","avatar_url":"https://github.com/evant.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Maven Central](https://maven-badges.herokuapp.com/maven-central/me.tatarka.parsnip/parsnip/badge.svg?style=flat)](https://maven-badges.herokuapp.com/maven-central/me.tatarka.parsnip/parsnip)\n\n# parsnip\n A modern XML library for Android and Java\n \n## Why parsnip?\n- Simple modern api similar to [gson](https://github.com/google/gson) and [moshi](https://github.com/square/moshi).\n- Based on kxml which is fast and memory efficient on Android.\n- More performant than SimpleXML while requiring less annotations in the common case.\n\n## Usage\n\n### Download\n\n```groovy\ncompile 'me.tatarka.parsnip:parsnip:0.3'\n```\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003eme.tatarka.parsnip\u003c/groupId\u003e\n    \u003cartifactId\u003eparsnip\u003c/artifactId\u003e\n    \u003cversion\u003e0.2\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\nThere is also a retrofit converter\n```groovy\ncompile 'me.tatarka.parsnip:parsnip-retrofit-converter:0.3'\n```\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003eme.tatarka.parsnip\u003c/groupId\u003e\n    \u003cartifactId\u003eparsnip-retrofit-converter\u003c/artifactId\u003e\n    \u003cversion\u003e0.2\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n### Parsing into objects\n```java\nString xml = ...;\n\nXml xml = new Xml.Builder().build();\nXmlAdapter\u003cBlackjackHand\u003e xmlAdapter = xml.adapter(BlackjackHand.class);\n\nBlackjackHand blackjackHand = xmlAdapter.fromXml(xml);\n```\n\n### Serialize objects into xml\n```java\nBlackjackHand blackjackHand = new BlackjackHand(\n    new Card('6', SPADES),\n    Arrays.asList(new Card('4', CLUBS), new Card('A', HEARTS)));\n\nXml xml = new Xml.Builder().build();\nXmlAdapter\u003cBlackjackHand\u003e xmlAdapter = xml.adapter(BlackjackHand.class);\n\nString xml = xmlAdapter.toXml(blackjackHand);\n```\n\n### Built in xml adapters\nParsnip has built-in support for reading and writing\n- primitive types\n- arrays, collections and lists\n- Strings\n- enums\n\nIt supports classes by writing them out field-by-field. Primitives will be written out as attributes by default, classes will be written out as tags.\n\nIf you have these classes:\n```java\nclass BlackjackHand {\n  public final Card hiddenCard;\n  public final List\u003cCard\u003e visibleCard;\n  ...\n}\n\nclass Card {\n  public final char rank;\n  public final Suit suit;\n  ...\n}\n\nenum Suit {\n  CLUBS, DIAMONDS, HEARTS, SPADES;\n}\n```\n\nParsnip will read and write this xml:\n```xml\n\u003cBlackjackHand\u003e\n  \u003chiddenCard rank=\"6\" suit=\"SPADES\"/\u003e\n  \u003cvisibleCard rank=\"4\" suit=\"CLUBS\"/\u003e\n  \u003cvisibleCard rank=\"A\" suit=\"HEARTS\"/\u003e\n\u003c/BlackjackHand\u003e\n```\n\n### Custom naming\nYou can customize the names of tags and attributes with `@SerializedName()`. The above example will look a little better as such:\n```java\nclass BlackjackHand {\n  @SerializedName(\"HiddenCard\")\n  public final Card hiddenCard;\n  @SerializedName(\"VisibleCard\")\n  public final List\u003cCard\u003e visibleCards;\n  ...\n}\n\nclass Card {\n  public final char rank;\n  public final Suit suit;\n  ...\n}\n\nenum Suit {\n  CLUBS, DIAMONDS, HEARTS, SPADES;\n}\n```\n\n```xml\n\u003cBlackjackHand\u003e\n  \u003cHiddenCard rank=\"6\" suit=\"SPADES\"/\u003e\n  \u003cVisibleCard rank=\"4\" suit=\"CLUBS\"/\u003e\n  \u003cVisibleCard rank=\"A\" suit=\"HEARTS\"/\u003e\n\u003c/BlackjackHand\u003e\n```\n\n### Text\nYou can use the `@Text` annotation to read/write the text of a tag.\n```java\nclass Card {\n  @Text\n  public final char rank;\n  public final Suit suit;\n}\n```\n```xml\n\u003cCard suit=\"SPADES\"\u003e6\u003c/Card\u003e\n```\n\n### Tag\nOften times you only care about the contents of a tag, not any of it's attributes. You can save some nesting in your hiarchy with the `@Tag` annotation.\n```java\nclass Card {\n  @Tag\n  public final char rank;\n  @Tag\n  public final Suit suit;\n}\n```\n```xml\n\u003cCard\u003e\n \u003crank\u003e6\u003c/rank\u003e\n \u003csuit\u003eSPADES\u003c/suid\u003e\n\u003c/Card\u003e\n```\n\n### Namespace\nBy default, any namespace on an element will be ignored. If you want to enforce a namespace, you can use the `@Namespace` annotation.\n\n```java\nclass Card {\n  @Namespace(\"http://example.com\", alias=\"ns\")\n  public final char rank;\n}\n```\nwill read\n```xml\n\u003cCard xmlns:ns=\"http://example.com\" rank=\"ignored\" ns:rank=\"6\"/\u003e\n```\nas `6`.\n\nWhen writing xml, the given alias will be used.\n\n## License\n\n    Copyright 2015 Evan Tatarka\n    \n    Licensed under the Apache License, Version 2.0 (the \"License\");\n    you may not use this file except in compliance with the License.\n    You may obtain a copy of the License at\n    \n       http://www.apache.org/licenses/LICENSE-2.0\n    \n    Unless required by applicable law or agreed to in writing, software\n    distributed under the License is distributed on an \"AS IS\" BASIS,\n    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n    See the License for the specific language governing permissions and\n    limitations under the License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fevant%2Fparsnip","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fevant%2Fparsnip","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fevant%2Fparsnip/lists"}