Skip to main content

A python library and command-line tool to "prettify" and colorize XML.

Project description

A python library and command-line tool to “prettify” and colorize XML. It also provides a unittest.TestCase mixin that adds the assertXmlEqual method and, on difference, shows a “pretty” diff.

Installation

$ pip install pxml

On the Command-Line

$ echo '<root><node attr="value">foo</node></root>' | pxml
<?xml version="1.0" encoding="UTF-8"?>
<root>
  <node attr="value">foo</node>
</root>

And add some color:

pxml with color

As a Python Module

import pxml, six

src = six.StringIO('<root><node attr="value">foo</node></root>')
out = six.StringIO()

pxml.prettify(src, out)

assert(out.getvalue() == '''\
<?xml version="1.0" encoding="UTF-8"?>
<root>
  <node attr="value">foo</node>
</root>
''')

Unit Testing

The pxml.XmlTestMixin class adds the assertXmlEqual method to the subclass which allows easy semantic comparison that two XML structures are equivalent. It does so by ignoring ignorable whitespace, attribute order, quote types, and other differences that are byte-level differences when serialized, but don’t actually represent semantic differences. When differences are detected, displays the XML differences in “prettified” XML for easier comparison.

import unittest, pxml

class MyTestCase(unittest.TestCase, pxml.XmlTestMixin):

  def test_equivalent_xml(self):
    src = '<root  ><node a="1" b="0"/></root>'
    chk = '<root><node   b="0" a="1"  /></root  >'
    self.assertXmlEqual(src, chk)

  def test_different_xml(self):
    src = '<root  ><node a="1" b="0"/></root>'
    chk = '<root><node   b="1" a="0"  /></root  >'
    self.assertXmlEqual(src, chk)

    # this fails the test and produces the following error message:
    #   AssertionError: [truncated]... != [truncated]...
    #     <?xml version="1.0" encoding="UTF-8"?>
    #     <root>
    #   -   <node a="1" b="0"/>
    #   +   <node a="0" b="1"/>
    #     </root>

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

pxml-0.2.13.tar.gz (21.3 kB view details)

Uploaded Source

File details

Details for the file pxml-0.2.13.tar.gz.

File metadata

  • Download URL: pxml-0.2.13.tar.gz
  • Upload date:
  • Size: 21.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pxml-0.2.13.tar.gz
Algorithm Hash digest
SHA256 e4752816e3353e8456b83a22c9c29bfef55e088ddc94722a4ece4766b6fb3f31
MD5 e480bfbe16bcb4886d57ec73ac3ba636
BLAKE2b-256 d0b7fbb739a8c43089289250b77125eaf858d8bdb085b234a26de987bee30931

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