Giter Site home page Giter Site logo

pxml's Introduction

Pretty-Print XML

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>

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.