Skip to main content

XML as plain object module

Project description

xmlplain

XML as plain object utility module

Build Status

Synopsys

This module is a set of utility functions for parsing XML input into plain list/dict/string types.

These plain XML objects in turn can be emitted through YAML for instance as bare YAML without python objects.

The motivating usage is to dump XML to YAML, manually edit files as YAML, and emit XML output back.

The original XML file is supposed to be preserved except for comments and (if requested) spaces between non-leaf elements.

Note that there are alternative modules with nearly the same functionality, but none of them both provide simple plain objects and preserve the initial XML content even for non structured XML.

WARNING: the implementation does not support XML namespaces and entity location. If this is found useful, please add an issue.

Usage

In order to convert a XML file to a YAML representation, for instance given the tests/example-1.xml file:

<example>
  <doc>This is an example for xmlobj documentation. </doc>
  <content version="beta">
    <kind>document</kind>
    <class>example</class>
    <structured/>
    <elements>
      <item>Elt 1</item>
      <doc>Elt 2</doc>
      <item>Elt 3</item>
      <doc>Elt 4</doc>
    </elements>
  </content>
</example>

Execute the following python3 code:

import xmlplain

# Read to plain object
with open("tests/example-1.xml") as inf:
  root = xmlplain.xml_to_obj(inf, strip_space=True, fold_dict=True)

# Output plain YAML
with open("example-1.yml", "w") as outf:
  xmlplain.obj_to_yaml(root, outf)

This will output the YAML representation in example-1.yml:

example:
  doc: 'This is an example for xmlobj documentation. '
  content:
    '@version': beta
    kind: document
    class: example
    structured: ''
    elements:
    - item: Elt 1
    - doc: Elt 2
    - item: Elt 3
    - doc: Elt 4

One can then read the emitted YAML representation and generate again an XML output with:

import xmlplain

# Read the YAML file
with open("example-1.yml") as inf:
  root = xmlplain.obj_from_yaml(inf)

# Output back XML
with open("example-1.new.xml", "w") as outf:
  xmlplain.xml_from_obj(root, outf, pretty=True)

This will output back the following XML (there may be some indentation and/or short empty elements differences w.r.t. the original):

<?xml version="1.0" encoding="UTF-8"?>
<example>
  <doc>This is an example for xmlobj documentation. </doc>
  <content version="beta">
    <kind>document</kind>
    <class>example</class>
    <structured></structured>
    <elements>
      <item>Elt 1</item>
      <doc>Elt 2</doc>
      <item>Elt 3</item>
      <doc>Elt 4</doc>
    </elements>
  </content>
</example>

For a detailled usage, read the API documentation with:

pydoc xmlplain

Or get to the online documentation at: https://guillon.github.io/xmlplain

Download

Download this module archives from the releases at: https://github.com/guillon/xmlplain/releases

Or get the git repository at: https://github.com/guillon/xmlplain

Requirements

The module is compatible with python 2.6/2.7 and python 3.x.

It depends upon the folowing modules:

sudo pip install --user setuptools PyYAML

On deb systems one may install these modules with:

sudo apt-get install python-setuptools python-yaml

Installation

Either copy the xmlplain.py file somewhere or install it with setup.py.

For a user local installation (installs to $HOME/.local) do:

./setup.py install --user

For a system level installation do:

./setup.py build
sudo ./setup.py install

Unitary tests

This module is delivered as part of a source tree with tests, in order to run tests, do for instance:

make -j16 check

Documentation

The documentation is generated with sphinx as is:

make doc

Install sphinx with:

pip install sphinx

Or on deb systems:

sudo apt-get install python-sphinx

The online documentation is hosted at: https://guillon.github.io/xmlplain

License

This is free and unencumbered software released into the public domain.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xmlplain-1.2.0.tar.gz (85.4 kB view details)

Uploaded Source

Built Distributions

xmlplain-1.2.0-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

xmlplain-1.2.0-py2-none-any.whl (10.2 kB view details)

Uploaded Python 2

File details

Details for the file xmlplain-1.2.0.tar.gz.

File metadata

  • Download URL: xmlplain-1.2.0.tar.gz
  • Upload date:
  • Size: 85.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.5.2

File hashes

Hashes for xmlplain-1.2.0.tar.gz
Algorithm Hash digest
SHA256 934c4dcd42424055599f9447a5958ddd8e696dfa05b3e9c22c0091b6fb5459d1
MD5 5aad4202eaf067e963d786536f8f19c8
BLAKE2b-256 64f6b3e5c9b9735d8543bc7b5f881e89b652eb08a579c0e50a0c6a3740e2912a

See more details on using hashes here.

File details

Details for the file xmlplain-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: xmlplain-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 10.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.5.2

File hashes

Hashes for xmlplain-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ff7ace68059f222074386a084abc9063168e75206e05a8e0655cf879ae3e74fe
MD5 2e3756f78082382daad030d0e9d96e39
BLAKE2b-256 80c8e8551336f2bb60674901e0e600c18992fca78572a5bb64c3ea0590faa878

See more details on using hashes here.

File details

Details for the file xmlplain-1.2.0-py2-none-any.whl.

File metadata

  • Download URL: xmlplain-1.2.0-py2-none-any.whl
  • Upload date:
  • Size: 10.2 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.5.2

File hashes

Hashes for xmlplain-1.2.0-py2-none-any.whl
Algorithm Hash digest
SHA256 c6316577eb75fa4da61bcc1f495b3d40b53e923b51b0868631aaafdfccbd92e3
MD5 40d4ee1c842c0236ad7b531894af4c83
BLAKE2b-256 c09761923e3587560e9afb6e7ea83ed0e1e0bb75ade642bf62a1559c694a7612

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page