简体   繁体   中英

How to pretty print XML from Smalltalk?

I have a String that contains XML, with no line feeds or indentations. I would like to turn it into a String with nice formatting plus syntax highlight. I don't want to use a web framework or web browser for such a simple thing. How do I do this?

| unformattedXml formattedXml |
unformattedXml := '<tag><nested>hello</nested></tag>'.
formattedXml := UnknownClass new format: unformattedXml.

Note: My input is a String. My output is a String.

To pretty print your xml string you can use the following

xmlString := '<a><b><c></c></b></a>'.
xml := XMLDOMParser parseDocumentFrom: xmlString.
^ String streamContents: [:stream|
    xml prettyPrintOn: stream ]

That should give you

<a>
    <b>
        <c />
    </b>
</a>

I don't know what would be the best option for syntax highlighting

Take a look at the XML packages on squeaksource.com , ss3.gemstone.com and smalltalkhub.com . There's bound to be something suitable for pretty printing.

Syntax highlighting is a different story since it depends on the medium you want to print on. What is your goal? You say that you don't want to use a web browser so I can only assume you want syntax highlighting in the Smalltalk environment? If so, you might have to resort to PetitParser and possibly write your own parser (you probably don't want to do that). Maybe you could give us some more details about what you are trying to accomplish?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM