• Skip to main content
  • Skip to primary sidebar

CodeBlogMoney

Make Money using Coding and Blogging

pretty print

How to print XML? Simple 2 step process

June 4, 2020 by Jimmy

How to print XML?

How to print XML? what kind of blog is this? is there a use of printer nowadays? hmm…

So the right question would be where do you print the XML? on browser console? on PDF ? on Papar (really!!)? or just want to format the XML data.

Let’s start with printing xml on browser console.

Print XML on Browser’s Console

Here is the code which uses vkbeautify javascript library which prints XML into the console.

<!DOCTYPE html>
<html>
  <body>
    <script src="https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/vkbeautify/vkbeautify.0.99.00.beta.js"></script>
    <script>
      var parser, xmlDoc;
      var text =
        "<employees><employee><id>1</id><firstName>Leonardo</firstName><lastName>DiCaprio</lastName></employee></employees>";

      console.log(vkbeautify.xml(text));
    </script>
  </body>
</html>

Copy the HTML code and create .html file, open in browser and open console to see the beautiful XML will be in Browser console log.

Print XML on PDF or Paper

Here is a way to create XML PDF. I don’t want to go into complex coding do to this. I have developed a tool which will help to do the same.

Follow this steps.

Go to XML Pretty Print tool.

how to print xml, open, https://jsonformatter.org/xml-pretty-print
ho Copy and paste you XML in Input XML Text area or click on Sample link

Click on XML Print button, which will open a print view of XML. Now you have the option to Print or Save as PDF.

how to print XML which helps to print in PDF or on Paper.
Print view with XML data.

This article helps to Print beautiful XML on Brower Console, PDF and on Paper.

Advance topic XML from URL and File.

Now we are going for more advance into this topic what if you have URL or File which you want to beautify and print?

Here are the steps for the XML from URL.

XML from URL

Go to XML Pretty Print tool. Click on “Load Data” button, which will open a dialogue box to enter a URL string into Text.

Paste your XML URL into the textbox and hit Load URL. It will fetch the data from the URL and update the Input XML text area. Click on XML Print to Write XML to PDF or Paper.

Load XML data from URL
Upload data Popup

XML from File

Go to XML Pretty Print tool. Click on “Load Data” button, which will open a dialogue box.

Click Upload File button which will open a file selector. Select XML file and Hit Open.

XML file will be uploaded and loaded in the Input XML area.

Click XML Print to Print XML to PDF or Paper.

Follow https://codeblogmoney.com/validate-json-string-using-javascript/

Filed Under: XML Tagged With: Javascript, pretty print

XML Pretty Print using Python – with Examples

May 30, 2018 by Jimmy

XML Pretty Print using Python is required frequently for testing, analyzing and debugging XML data. Python and XML both are treading in programming fields. Python generates dynamic XML string and received by the client. To save memory or bandwidth it’s better to send the minified version of XML text.

Some time for debugging purposes, we need to see beautified XML data to understand and to Debug. This article will help to pretty-print XML data.

There are two examples in the post.

  1. Pretty Print XML String
  2. Pretty Print XML File

1. XML Pretty Print using Python

Here is an example:

import xml.dom.minidom

uglyxml = '<?xml version="1.0" encoding="UTF-8" ?><employees><employee><Name>Leonardo DiCaprio</Name></employee></employees>'

xml = xml.dom.minidom.parseString(uglyxml)

xml_pretty_str = xml.toprettyxml()

print xml_pretty_str

Result :

<?xml version="1.0" ?>
<employees>
  <employee>
    <Name>Leonardo DiCaprio</Name>
  </employee>
</employees>

Here is the explanation for the code.

  1. import xml.dom.minidom

This is an XML library available in python to convert the DOM object from XML string or from the XML file.

  1. xml.dom.minidom.parseString

parseString is a method which converts XML string to objectxml.dom.minidom.Document.

  1. xml.toprettyxml

toprettyxml This method will indent the XML data and return as Pretty XML string.

2. XML Print Print from File using Python

Here is the Example:

import xml.dom.minidom

with open('xmlfile.xml') as xmldata:
    xml = xml.dom.minidom.parseString(xmldata.read())  # or xml.dom.minidom.parseString(xml_string)
    xml_pretty_str = xml.toprettyxml()

print xml_pretty_str

This program reads from file and load and create XML Document objects and print the XML data in beautiful XML String.

I have used these tools for creating this blog post.

XML Formatter

XML Viewer

Related articles:

JSON Pretty Print using Python

Filed Under: Python, XML Tagged With: pretty print, python, XML

JSON Pretty Print using Python- with Examples

May 25, 2018 by Jimmy

JSON Pretty Print using Python is required frequently for testing, analyzing and debugging JSON data. Python and JSON both are treading in programming fields. Python generates dynamic JSON string and received by the client. To save memory or bandwidth it’s better to send the minified version of JSON text.

Some time for debugging purpose, we need to see beautified JSON data to understand and to Debug. This article will help to pretty print JSON data.

There are two examples in the post.

  1. Pretty Print JSON String
  2. Pretty Print JSON File

1. JSON Pretty Print using Python

Here is an Example:

import json

uglyjson = '{"firstnam":"James","surname":"Bond","mobile":["007-700-007","001-007-007-0007"]}'

#json.load method converts JSON string to Python Object
parsed = json.loads(uglyjson)

print json.dumps(parsed, indent=2, sort_keys=True)

Result :

{
  "firstnam": "James", 
  "mobile": [
    "007-700-007", 
    "001-007-007-0007"
  ], 
  "surname": "Bond"
}

Here is the explanation for the code.

  1. import json

This is a JSON library available in python to convert Python object from JSON string or from JSON file.

  1. json.load

json.load is a method which converts JSON string to Python object. Here is the conversion table form JSON data type to Python data types.

JSON Python
object dict
array list
string unicode
number (int) int,long
number (real) float
true True
false False
null None
  1. json.dumps

json.dums is the opposite of json.load. It creates a JSON string from Python object. It takes parameters indent parameter which formats the JSON string.

2. JSON Print Print from File using Python

Here is the Example:

import json

with open('jsonfile.txt') as jsonfile:
    parsed = json.load(jsonfile)

print json.dumps(parsed, indent=2, sort_keys=True)

This program reads from file and load and create Python objects and dumps the JSON data in beautiful JSON String. Read JSON File using Python for more details

I have used these tools for creating this blog post.

JSON Editor

JSON Viewer

 

Filed Under: JSON, Python Tagged With: json, pretty print, python

Primary Sidebar

Categories

  • Blogging
  • HTML
  • Java
  • JavaScript
  • jQuery
  • JSON
  • MySql
  • Performance
  • PHP
  • Problem Solving
  • Python
  • Testing
  • XML

Copyright © 2021 · Metro Pro on Genesis Framework · WordPress · Log in