Not able to display XML in Wordpress post
Asked Answered
W

4

7

If you want to display an XML fragment in a WordPress post - how do you do that?

Suppose you want to display this:

<family>
    <dad>whatever</dad>
    <mom>whatever</mom>
</family>

Using the [sourcecode language="xml"] does not help, as it mangles up the XML. Also, I believe, plugins cannot be used with free version of WordPress ( i.e wordpress.com ) - so, probably that won't be an option.

Using HTML <pre> tag works, but it does not give correct look and feel. Can any one show me how they have done it?

Wry answered 15/2, 2013 at 14:20 Comment(0)
V
4

The wordpress codex has a whole page about writing code in your posts.

http://codex.wordpress.org/Writing_Code_in_Your_Posts

Their suggestion is to use html entity codes within <pre> or <code> tags

&lt;family&gt;
    &lt;dad&gt;whatever&lt;/dad&gt;
    &lt;mom&gt;whatever&lt;/mom&gt;
&lt;/family&gt;

This could be tedious if you have a lot of code to show in your post. I suggest writing a small shortcode that would do this for you.

<?php
function xml_shortcode( $atts, $content ) {
    return '<pre>' . htmlentities( $content ) . '</pre>';
}

add_shortcode( 'xml', `xml_shortcode` );
?>

How to use your shortcode in a post

[xml]your code here[/xml]
Vern answered 15/2, 2013 at 14:31 Comment(2)
As i stated in my question: <pre> is the only one that works, everything else just garbles my xml once i switch between Text to View Mode and back. And pre is not good becz of its lack of formattingWry
htmlentities() function adds <br /> tag at the end of each line. The output looks like: <custom></<cusom><br /> . Any idea how to solve this issue?Telekinesis
M
0

If [sourcecode] is available but you cannot install additional plugins or add shortcodes (which is the case on wordpress.com), you have, to my knowledge, no other option than to encode the problematic characters before copying them.

Here are the detailed steps:

  1. Encode your snippet using an HTML encode, for instance using a free, online encoder
  2. edit your post in HTML mode
  3. add [sourcecode language="xml"]
  4. past the encoded source code
  5. add [/sourcecode]
Molluscoid answered 27/5, 2018 at 9:46 Comment(0)
D
0

I suffered from this problem recently.
For me what worked was to enter the code in text mode. Selection of code. Application of code formatting from the Format menu.

Later, I used the crayon syntax highlight plugin. The plugin works well, you have to follow the same process as above with the only difference that instead of code formatting option, choose the crayon option after selecting the code to highlight.

You can check the highlighting here

Despairing answered 30/5, 2018 at 9:0 Comment(0)
I
0

Wordpress.com now has built-in support for code highlighting in a variety of languages using the [code] shortcode. I find that I still have to use html entity codes when highlighting XML, but other languages work well (JavaScript, C#, etc.), and it looks much better to the reader than your basic <pre> or <code> tags.

Ilario answered 1/2, 2019 at 1:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.