GitHub - Jungbluth11/XmlViewer: XML viewer for Nextcloud · GitHub
Skip to content
Jungbluth11Public

About

XML viewer for Nextcloud

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

XML Viewer for Nextcloud

This Nextcloud app displays XML files from the Files app in a structured view when a parser for their XML structure is installed. Without a matching parser, the file is handed over to the native Nextcloud viewer.

Features

  • Displays XML files in the corresponding view when a matching parser is installed.
  • Hands XML files without a matching parser, unsupported XML content, and files larger than 5 MiB over to the native Nextcloud viewer.

The action in the Files app is offered based on the XML MIME type provided by Nextcloud. The XML root element then determines whether an installed parser can handle the file.

Requirements and compatibility

  • Nextcloud Server 33+ (for Nextcloud prior 33 see other branches)

Installation

  1. Extract the release archive into the Nextcloud apps directory. The folder must be named xmlviewer.
  2. Enable the app in Settings → Apps or by running occ app:enable xmlviewer.
  3. A parser for the relevant XML format must also be installed to display files in a structured view. Without a matching parser, the native Nextcloud viewer is used.

Creating a parser

A parser belongs in its own subdirectory under lib/Parser. The app automatically loads every file named Parser.php in those directories. The parser must implement XmlParserInterface and return an instance at the end of the file.

Example of a simple XML document:

<catalog>
    <entry name="Star chart" category="Tool" />
    <entry name="Travel journal" category="Book" />
</catalog>

lib/Parser/Catalog/Parser.php:

<?php
declare(strict_types=1);

namespace OCA\XmlViewer\Parser\Catalog;

use OCA\XmlViewer\Parser\XmlParserInterface;

class Parser implements XmlParserInterface {
	public function supportsRoot(string $rootNode): bool {
		return $rootNode === 'catalog';
	}

	public function parse(\SimpleXMLElement $xml, string $source): ?array {
		if (!$this->supportsRoot($xml->getName())) {
			return null;
		}

		$items = [];
		foreach ($xml->entry as $entry) {
			$items[] = [
				'name' => (string)$entry['name'],
				'category' => (string)$entry['category'],
			];
		}

		return ['items' => $items];
	}

	public function getTemplatePath(): string {
		return __DIR__ . '/template.php';
	}
}

return new Parser();

The template at lib/Parser/Catalog/template.php receives the returned array as $_['data'] and can display it like this:

<ul>
<?php foreach ($_['data']['items'] as $item): ?>
    <li><?php p($item['name']); ?> (<?php p($item['category']); ?>)</li>
<?php endforeach; ?>
</ul>

You can optionally add a style.css file to the same parser directory (lib/Parser/Catalog/style.css). The app loads it with the parser's view, so you can use it to style the HTML generated by the template.

You can also add a script.js file to that directory. The shared viewer detects it and loads it automatically for documents handled by that parser. Use it for parser-specific interactions such as tab behavior; do not add a script('xmlviewer', ...) call to the parser template.

supportsRoot() should return true only for the root element of the supported XML format. parse() returns data prepared for the template, or null if the document cannot be processed. The template renders the data; use Nextcloud's p() output function for dynamic values.

License

AGPL-3.0-or-later. See the GNU Affero General Public License v3.

About

XML viewer for Nextcloud

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Contributors

Languages