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>
to
<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"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" encoding="UTF-8" indent="yes"/>

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

</xsl:stylesheet>
XSL

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

But nokogiri has another problem, which unwrap CDATA.

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

-- 
Posted via http://www.ruby-forum.com/.

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

See related articles to this posting


0 resp.  ToT

-- 
Posted via http://www.ruby-forum.com/.

0
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,
   zimbatm




2011/1/12 Pk Z. <k@z-pk.com>:
> 0 resp. =C2=A0ToT
>
> --
> Posted via http://www.ruby-forum.com/.
>
>

0
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. <k@z-pk.com> wrote:

> 0 resp.  ToT
>
> --
> Posted via http://www.ruby-forum.com/.
>
>

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

3 Replies
489 Views

Similar Articles

[PageSpeed] 17


  • Permalink
  • submit to reddit
  • Email
  • Follow


Reply:

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...

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 (http://java.sun.com/j2se/1.4.2/docs/api/index.html). 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("{http://xml.apache.org/xslt}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. libXm.so.4), 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 <peter.hav...@gmail.com> wrote: > If I need to install a given file (e.g. libXm.so.4), 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> ...

Newbie
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...

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...

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. http://www.w3schools.com/xml/note.xml) 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; xmlhttp.open("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 - http://xarchiver.sourceforge.net Cpsed, a Linux OpenGL 3D scene editor - http://cpsed.sourceforge.net 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...

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 ...

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 ========================================================================...

Easiest way to read a XML doc from file, reformat it and write it indented to a second file?
Assume I have a XML docuemnt in file mydoc123.xml The XML doc is textually formatted as one long line/string without line breaks. Now I want to read this XML doc file into Java, then reformat it so that the hierarchy levels are indented in a human radble format "as usual". Finally the result should be written to another text file result.xml How can I do this most easily (preferable without third party libraries but only J2SE built-in functions)? Ray Raymond Schanks wrote: > Now I want to read this XML doc file into Java, then reformat it so > tha...

what is the best way to find a key.
Given that you have a plaintext, output of sha1sum, output of strace, ltrace, strings. And also a suid program that perfoms something like "cat secretfile input | sha1sum | cat secretfile - | sha1sum" . Given that myself is an onrdinary user that invokes this program by suplying a filename - whose output is eched to the screen. I want to find a key used in that program. Any suggestions? Thanks puzzlecracker wrote: > Given that you have a plaintext, output of sha1sum, output of strace, > ltrace, strings. And also a suid program that perfoms something like > "ca...

Best way to access this file?
Hello, I have a text file I'm attempting to parse. There are about 50 fixed width fields in each line / row. For example (shortened for brevity): W1234Somebody East 101110001111010101 E1235Someone Else West 010111001001010101 I'm having problems pulling these fields into structures, in order to be able to access each individually. I am currently opening as a sequential file. Is there a better way? My structure looks something like: struct data{ char area[1]; char empNumber[4]; char name[16] char region[5]; char options[20]; } int index = 0; data user[100...

Best way to open files
I have implemented a class which performs operations on some file throughout different methods in the class. I did this by making the ifstream a private class member and opening the file in the constructor, however someone told me this was bad practice to have the stream as a class attribute. what is the best way to do it? On 2005-04-08, Dave Mill <davemill@davemill.com> wrote: > I have implemented a class which performs operations on some file > throughout different methods in the class. > > I did this by making the ifstream a private class member and opening th...