Finding best way to pretty print XML files

  • Permalink
  • submit to reddit
  • Email
  • Follow

Havn't found best soultion to pretty print XML:

REXML: It has pp functionality, but it wrap elements like <a>content</a>

Nokogiri: there is a approch to pp, see ex:

# Pretty print XML
require "nokogiri"

def write(buffer)

  xsl =<<XSL
<xsl:stylesheet version="1.0"
  <xsl:output method="xml" encoding="UTF-8" indent="yes"/>

<xsl:strip-space elements="*"/>
<xsl:template match="/">
  <xsl:copy-of select="."/>


    doc = Nokogiri::XML(buffer)
    xslt = Nokogiri::XSLT(xsl)
    out =  xslt.transform(doc)

But nokogiri has another problem, which unwrap CDATA.

Any better solution? Thanks. And apologize for my poor english.

Posted via

Reply Pk 1/11/2011 7:35:56 AM

See related articles to this posting

0 resp.  ToT

Posted via

Reply Pk 1/12/2011 12:53:30 AM

Sorry if you haven't got any help,

could you provide more informations to the output you want ?

There are many ways to indent XML code. Maybe you don't want to wrap lines
with <a>content</a>, but what if the content is longer ? Do you have a
column limit ?

In your place, I would take the pp code of nokogiri and tweak it until it
gives me what I want.

Hope that help,

2011/1/12 Pk Z. <>:
> 0 resp. =C2=A0ToT
> --
> Posted via

Reply Jonas 1/12/2011 11:18:50 PM

[Note:  parts of this message were removed to make it a legal post.]

In my experience, everyone has a different definition of "pretty printing".
If you want to get more specific about what you'd like to see, perhaps
people can give you a more specific answer.

On Tue, Jan 11, 2011 at 7:53 PM, Pk Z. <> wrote:

> 0 resp.  ToT
> --
> Posted via

Reply Mike 1/13/2011 12:05:12 AM
comp.lang.ruby 48877 articles. 5 followers. Post

3 Replies

Similar Articles

[PageSpeed] 5

  • Permalink
  • submit to reddit
  • Email
  • Follow


Similar Artilces:

How to sort a XML file itself or how to compare two XML files whith pretty printed diff.
All, I got following question. I want to sort a XML files content in following manner: Source file: <btag> <bbtag key=val/> <abtag key=val/> </btag> <atag> </atag> <btag> <bbtag key=val/> <abtag key=val/> <cbtag key=val/> </btag> Target file: <atag> </atag> <btag> <abtag key=val/> <bbtag key=val/> </btag> <btag> <abtag key=val/> <bbtag key=val/> <cbtag key=val/> </btag> As order in my XML structures do not matter, the sorting my be based o...

IN your opinion what is the best way to share files between LInux and IN your opinion what is the best way to share files between LInux and MAC OS X?
IN your opinion what is the best way to share files between LInux and MAC OS X? I set up SAMBA on the LInux machine and the MAC (running the very latest OSX) can copy files to the SAMBA share at lightning speeds. When I try and copy files from the LInux machine to the Mac they take an eternity. So slow in fact I have to kill the process . I would appreciate any thoughts on why that maybe and also if there is a better way to share files (I guess HTTP-FTP would be the most sensible option ). navti wrote: > IN your opinion what is the best way to share files between LInux and > MAC O...

Best way to delete old files? find & exec VS find & xargs
Hello, We have an NFS partition exported though Solaris 9 and I have a question about removing files via a cron script. For a while I've been doing this: find /scratch -mtime +14 | xargs rm But I feel somehow limited by this, since this has problems with filenames with spaces in, and also can not remove directories. I can add the -R option, but I was wondering if the find /scatch -mtime +14 -exec rm \{\} \; would be better and is able to handle files with spaces and such in since I would like to do this as well for a shared Samba directory for tempfiles. Maybe running -exec rmdir \{...

really no way to pretty print XML???
Hi all I wonder if there is really no way to pretty print a DOM, using Java's standard API ( What I've tried is (assume having a org.w3c.dom.Document instance doc): TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer();transformer.setOutputProperty(OutputKeys.METHOD, "xml");transformer.setOutputProperty("encoding", encoding); transformer.setOutputProperty(OutputKeys.INDENT,"yes");transformer.setOutputProperty("{}i...

Best way to read an XML properties file?
Hi all,I hope all you Java gurus that have been around the block a time or two cananswer this simple question.What's the easiest way to read an XML properties file of relativecomplexity? I know about Properties.readFromXML, but I don't think it'llwork on my semi-complex property file.Here's my config file:<DW> <dir name="c:\x\y" subdirs="true"> <mask value="*.go"> <condition value="-*.go"/> <operation value="encrypt"/> <key value="key1"/> <iv value="1234123412...

Best way to find a package containing a file.
If I need to install a given file (e.g., but the package containing it isn't installed, what's the best way to discover the package it's in? I usually go to the installation media and search as in this example: find Solaris_10/Product -name pkgmap -exec grep -l libXm {} + Is there a better way? Thanks, Pete On Jan 23, 3:17=A0pm, Peter Havens <> wrote: > If I need to install a given file (e.g., but the package > containing it isn't installed, what's the best way to discover the > package it's in? I usuall...

Best way to filter XML file using gawkextlib
Suppose I have an XML file as follows and I want to filter it, to remove the 'flow sensor' readings but leave the thermometer readings and the rest of the XML structure intact to be passed on to a subsequent process as a valid XML file. <?xml version="1.0" encoding="UTF-8"?> <envelope> <header> <datetime>2014-02-25T07:56:52Z</datetime> <seq>123</seq> </header> <body> <sensor> <type>thermometer</type> <value>16.4</value> ...

Hi, I have some dumb questions, however I didn't find the answers in the FAQ. So, ok, now I have done my whole report in LaTeX. The result is great and I have inserted many pics. To stay compatible with LaTeX, I converted all my pics in eps. I also didn't used any exotic fonts (so, I use the fonts provided by default by LaTeX). As background info: I use MikTeX under WinXP. Here are my questions: Which file should I print if I have a postscript printer (HP LaserJet 4300-n) ? 1. The *.dvi generated by LaTeX. I would then print it using yap provided with MikTeX 2. The *.ps conv...

Best way to compare two XML files for equivalence (subset)
Hi, I have two XML files and I want to check each value in one file to see if it exists in the other file - following the same path. The XML files have equivalent schema definitions. Is there an easy way programmatically or using XSLT? Sample file to match with <MyXML> <Branch> <Item> <Nbr>12</Nbr> </Item> </Branch> <Empty/> <SomewhatEmpty> <Nothing/> </SomewhatEmpty> </MyXML> Sample file to match on <MyXML> <Branch> <Item> <Nbr>12</Nb...

Pretty printing the pretty print.
For the first time ever, I have needed to print some code fragments. The default print file command in the GNAT studio produced black and white plain text. Is there something that will produce a more "illuminated" output? -- "Why," said Ford squatting down beside him and shivering, "are you lying face down in the dust?" "It's a very effective way of being wretched," said Marvin. Mike ;-( On 25.09.13 12:36, Mike H wrote: > For the first time ever, I have needed to print some code fragments. The default print file command in the GNAT...

Which way is the best way to require another file in .rb
I put all source in to lib/ path, and named a lib/foo path for my foo project. However, is there a better approach to require the ruby source which in lib/foo/ from the file in the same path. For example, lib/foo/db.rb require lib/foo/configure.rb, currently I write: require "#{File.dirname(__FILE__)}/../configure" But it is a ugly way to require other files. Would anyone explain a more "standard" approach to do it? On (2008-04-12 02:35 +0900), Eddy Xu wrote: > require "#{File.dirname(__FILE__)}/../configure" > > But it is a ugly way to require othe...

Best method to access an external XML file (ie. using JS
Hi, Im trying to write some javascript where I can access an XML file from another website. Here is my code for doing so: <html> <head> <script type="text/javascript"> var xmlhttp; function loadXMLDoc(url) { xmlhttp=null; // code for Mozilla, etc. if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); } // code for IE else if (window.ActiveXObject) { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } if (xmlhttp!=null) { xmlhttp.onreadystatechange=state_Change;"GET",url,true); xmlhttp.send(null); } else { ale...

What is the best way to add 3rd party sources files, header files etc into CGIR S-function without TLC file?
CGIR can generate the code without using TLC, but usually we write extra call steps to include these 3rd party source files etc in the TLC file. But if we are not using TLC, but only CGIR S-functions, what is the best way? Thanks, ...

Whats the best way to fit a .wav file to midi file?
I'm doing a transcription of an old big band recording using Cakewalk Pro Audio 8. My plan is to have the wave file at the top and then piece together the band below it, following along with the recording. I want to make the wave file fit fairly precisely into the measures so I don't have to futz with the midi tracks later to make them line up. Tape decks being what they are and the original performance being done by humans, after a while it drifts off from the metronome beat. A way I've come up with is to find a tempo that fits for as long as possible, paste in the wave file and ...

Best way to include a jpg file in a project (where to locate the file)
Inside of my main VI - I am "building a path to the current VIs" directory where I have a jpg file. This jpg file displays fine when running this VI.&nbsp; To do this I get the current path, strip the VI name and then add the jpg name to the path. &nbsp; Now when I build an exe file and run it the jpg is not displayed.&nbsp; In the build specification it appears to force the jpg file to be in the support (or data)&nbsp;directory.&nbsp;&nbsp; Because of this it can no longer find the jpg when running in exe form.&nbsp; I also notice that the path returned w...

The best way to delete some files inside an ISO9660 image file
Hi, suppose I have a CD iso image file of 2GB and I want to delete some files inside it. What is the fastest and least disk space way to achieve it ? P.S. I don't want to mount the image, copy its content in /tmp/, delete the files and then mkisofs the content. I want to do it directly on the structure of the .iso file as rar and zip delete the files inside the archive. Thank you, -- Colossus Xarchiver, a GTK2 only archive manager - Cpsed, a Linux OpenGL 3D scene editor - Mizio, a QT proxy hunter scanner tool...

Best way to combine .g3 files into one ps/pdf file
I am using mgetty/sendfax to receive faxes. By default, mgetty/sendfax stores received faxes as individual image files for each page so that a three page fax ends up as three separate .g3 files. I'd like to combine those .g3 files into one ps or pdf file. Any suggestions on the best way to do this? TIA, --Kamus ["Followup-To:" header set to comp.os.linux.misc.] On Wed, 10 Nov 2004 11:27:56 -0500, Kamus of Kadizhar staggered into the Black Sun and said: > I am using mgetty/sendfax to receive faxes. By default, > mgetty/sendfax stores received faxes as individual image...

Whats the best way to add a bit of a help file into an access file
Say, on a data entry form a "HELP" cmbBUTTON that bounces the user off to word file that has help, or is there some other way to do it (like a label object with help on another form) Thanks Jerry Something I have done that works well and is very simple is to have a single table with 3 fields. One for the form name, one for a title and one for the help details. On each form that needs help - simple ones don't - you either add a button with the familiar ? character, or the word 'Help'. When the user clicks on it, it pops up a form that is filtered to the form name...

Best way to print
We have a database with a javascript frontend. This front end allows us to view and print records. We are looking for the "most efficient" way to print. I know there is a print() function, but is there another way to do it? There is no particular problem, we just want to make sure that we are printing the most efficient way. I know this falls outside this discussion group, but would anyone suggest something outside of javascript? Is there a better way/language? The way the system works, is it when it goes to print, it saves the information to be printed to a file on the server, and ...

XML pretty printing?
Hi... Is there something in Ruby that will make XML look nice on STDOUT? -Chris On Wed, 4 Jan 2006, Chris McMahon wrote: > Hi... > > Is there something in Ruby that will make XML look nice on STDOUT? > > -Chris puts doc.pretty -a -- =============================================================================== | ara [dot] t [dot] howard [at] noaa [dot] gov | all happiness comes from the desire for others to be happy. all misery | comes from the desire for oneself to be happy. | -- bodhicaryavatara ========================================================================...

Copy characterdata from XML file to XML file
Hi, I'm writing a Perl script that has to copy a block of data (nodes numbers and coordinates) from one XML formatted file into another XML file. I'm using XML::Parser to extract the data and XML::Writer to write the data into the second file. This does not work, since some of the numbers are corrupted after being read by XML::Parser. Below I have copied a small bit that shows how the data is corrupted. It always happens at the same line(s) of data. 67 2.9005093479606E+000 3.6637104002418E-001 7.9522656092442E-001 68 2.8852994122583E+000 3.5353599488296E-001 7.751659126573...

XML Project Guidance in ASP.Net environment: How to best produce XML file
I have a very basic ASP.Net application with SQL 2005 express back end. I need some direction on which way to proceed on a project I have been charged with. I need to be able to build an xml file on demand with a specified schema and elements that get populated from data entered through the web interface. I need to insert the data elements from the db into the xml file, grab actual documents from a data store (network folder) and zip all together into one file (xml file with data + actual pdf documents) for submission to an XML web service (provided by another party). Any guidance or outline ...

UPnP xml file to WSDL xml file
Hello, could somebody help me to convert the below UPnP service file to WSDL web service file: <serviceList> <service> <serviceType> urn:schemas-upnp-org:service:PowerSwitch:1 </serviceType> <serviceId> urn:upnp-org:service:PowerSwitch:1 </serviceId> <controlURL>/upnp/control/power1</controlURL> <eventSubURL>/upnp/control/power1</eventSubURL> <SCPDURL>/blenderpowerSCPD.xml</SCPDURL> </service> thanks in advance bobby I tried to combine UPnP servive and blenderpowerSCPD.xml and come up with WSDL w...

How to Print a "Print to File" File
Here is what I want to do: I printed a PDF file as "print to file" (File.PS) It is a PostScript Printer file (It comes to 265K) Now, I want to send the file "File.PS" to the printer on LPT1 so it will print. I tried >copy File.PS lpt1 BUT, it seems it sent something to the printer (Not long enough as when Adobe Reader send the PDF file to the printer) and nothing happened. I sent it again as above and about 2 inches of the page printed. Any suggestion how to do what I want? OR Why what I tried did not work? Paul -- ...