axmlfmt is an opinionated formatter for Android XML resources

axmlfmt

axmlfmt is an opinionated formatter for Android XML resources. It takes XML that looks like

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    tools:context=".MainActivity">


    <!-- Not using AppCompat to reduce app size -->
  <Toolbar
      android:layout_width="match_parent"
      android:id="@+id/toolbar"
      android:layout_height="wrap_content"
      android:title="@string/app_name"/>

    <ScrollView
        android:layout_weight="1"
        android:layout_width="match_parent"
      android:layout_height="0dp"
        android:fillViewport="true">

        <EditText
                android:id="@+id/text"
            android:layout_width="match_parent"
            android:importantForAutofill="no"
            android:layout_height="wrap_content"
            android:textSize="16sp">
          </EditText>

    </ScrollView>
</LinearLayout>

and turns it into

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <!-- Not using AppCompat to reduce app size -->
    <Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:title="@string/app_name" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:fillViewport="true"
        android:layout_weight="1">

        <EditText
            android:id="@+id/text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:importantForAutofill="no"
            android:textSize="16sp" />
    </ScrollView>
</LinearLayout>

Install

Pre-compiled binaries of axmlfmt can be downloaded from the releases page. Alternatively, you can install it with with the go command by running:

go install github.com/rsookram/axmlfmt/cmd/axmlfmt@latest

Usage

One way you may want to use axmlfmt is to have it format all the XML files in a git repository. This can be done with the following command:

git ls-files '*.xml' | xargs axmlfmt -w

The full usage description is:

USAGE:
    axmlfmt [FLAGS] [FILE]...

FLAGS:
    -h, -help, --help    Prints help information
    -V                   Prints version information
    -w                   Writes result to (source) file instead of stdout

ARGS:
    <FILE>...    Path of XML files to format

Build

axmlfmt can be built from source by cloning this repository and using the go command.

git clone https://github.com/rsookram/axmlfmt
cd axmlfmt
go build ./cmd/axmlfmt

License

Copyright 2019 Rashad Sookram

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Owner
Rashad Sookram
Mostly doing Android development
Rashad Sookram
Similar Resources

Freestyle xml parser with golang

fxml - FreeStyle XML Parser This package provides a simple parser which reads a XML document and output a tree structure, which does not need a pre-de

Jul 1, 2022

🧑‍💻 Go XML generator without Structs™

exml 🧑‍💻 Go XML generator without Structs™ Package exml allows XML documents to be generated without the usage of structs or maps. It is not intende

Nov 15, 2022

wikipedia-jsonl is a CLI that converts Wikipedia dump XML to JSON Lines format.

wikipedia-jsonl wikipedia-jsonl is a CLI that converts Wikipedia dump XML to JSON Lines format. How to use At first, download the XML dump from Wikime

Dec 26, 2022

Quick and simple parser for PFSense XML configuration files, good for auditing firewall rules

pfcfg-parser version 0.0.1 : 13 January 2022 A quick and simple parser for PFSense XML configuration files to generate a plain text file of the main c

Jan 13, 2022

Sqly - An easy-to-use extension for sqlx, base on xml files and named query/exec

sqly An easy-to-use extension for sqlx ,base on xml files and named query/exec t

Jun 12, 2022

This service will help you detect any waste of resources in your AWS account

Frugal-Hero This service will help you detect any waste of resources in your AWS account. The policy is: if it is not useful, delete it! Requirements

Jan 31, 2022

The android-go project provides a platform for writing native Android apps in Go programming language.

The android-go project provides a platform for writing native Android apps in Go programming language.

android-go The android-go project aims to provide a platform (namely an SDK) for writing native Android apps in Go programming language. All things he

Jan 5, 2023

End-to-end encrypted file transfer for Android. An Android Magic Wormhole client.

End-to-end encrypted file transfer for Android. An Android Magic Wormhole client.

wormhole-william-mobile This is a Magic Wormhole client for Android. (Perhaps someday this will also support iOS). Some current limitations: Receiving

Jan 4, 2023

androidqf (Android Quick Forensics) helps quickly gathering forensic evidence from Android devices, in order to identify potential traces of compromise.

androidqf androidqf (Android Quick Forensics) is a portable tool to simplify the acquisition of relevant forensic data from Android devices. It is the

Dec 28, 2022

Wg-android - Android GUI for WireGuard

Android GUI for WireGuard Download from the Play Store This is an Android GUI fo

Feb 10, 2022

Esfmt - An opinionated, zero-configuration formatter for ES/TS/ESX/TSX

esfmt - an opinionated, zero-configuration formatter for ES/TS/ESX/TSX Status: t

Apr 5, 2022

Go encoding/xml package that improves support for XML namespaces

encoding/xml with namespaces This is a fork of the Go encoding/xml package that improves support for XML namespaces, kept in sync with golang/go#48641

Nov 11, 2022

Gosaxml is a streaming XML decoder and encoder, similar in interface to the encoding/xml

gosaxml is a streaming XML decoder and encoder, similar in interface to the encoding/xml, but with a focus on performance, low memory footprint and on

Aug 21, 2022

Arche - Smart Hybrid Workforce Manager: A system that aims to provide companies an easy to use platform for managing company resources by allowing employees to book company spaces and resources.

Arche - Smart Hybrid Workforce Manager: A system that aims to provide companies an easy to use platform for managing company resources by allowing employees to book company spaces and resources.

Description Smart Hybrid Workforce Manager is a system that aims to provide companies an easy to use system for managing company resources by allowing

Dec 8, 2022

Console JSON formatter with query feature

Console JSON formatter with query feature

Console JSON formatter with query feature. Install: $ go get github.com/miolini/jsonf Usage: Usage of jsonf: -c=true: colorize output -d=false: de

Dec 4, 2022

A shell parser, formatter, and interpreter with bash support; includes shfmt

sh A shell parser, formatter, and interpreter. Supports POSIX Shell, Bash, and mksh. Requires Go 1.14 or later. Quick start To parse shell scripts, in

Dec 29, 2022

A golang formatter that fixes long lines

golines Golines is a golang formatter that shortens long lines, in addition to all of the formatting fixes done by gofmt. Motivation The standard gola

Jan 6, 2023

A shell parser, formatter, and interpreter with bash support; includes shfmt

sh A shell parser, formatter, and interpreter. Supports POSIX Shell, Bash, and mksh. Requires Go 1.14 or later. Quick start To parse shell scripts, in

Jan 8, 2023

Small Clojure interpreter, linter and formatter.

Small Clojure interpreter, linter and formatter.

Joker is a small Clojure interpreter, linter and formatter written in Go. Installation On macOS, the easiest way to install Joker is via Homebrew: bre

Dec 30, 2022
Comments
  • Preserve XML entities in char data

    Preserve XML entities in char data

    They're currently being replaced. For example, the input

    <resources>
    
        <string name="and">&amp;</string>
    </resources>
    

    gets formatted as

    <resources>
    
        <string name="and">&</string>
    </resources>
    
  • Improve handling of XML namespaces

    Improve handling of XML namespaces

    Currently the only namespaces handled are android, app, and tools: https://github.com/rsookram/axmlfmt/blob/fc6ddf3830bb9f8655818f19fa6cae65d2051766/internal/printer/printer.go#L197-L204

    This results in two problems:

    1. If an alternate name is used, it will be replaced for the attributes, but not in the namespace declaration.

      This means that

      <androidx.cardview.widget.CardView
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:card_view="http://schemas.android.com/apk/res-auto"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          card_view:cardCornerRadius="4dp" />
      

      gets reformatted to

      <androidx.cardview.widget.CardView
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:card_view="http://schemas.android.com/apk/res-auto"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          app:cardCornerRadius="4dp" />
      
    2. Other namespaces like aapt aren't handled, so

      
      <vector
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:aapt="http://schemas.android.com/aapt"
          android:width="108dp"
          android:height="108dp"
          android:viewportWidth="108"
          android:viewportHeight="108">
      
          <path android:pathData="L75.97,107.97L32,64Z">
              <aapt:attr name="android:fillColor">
                  <gradient
                      android:endX="78.5885"
                      android:type="linear">
                      <item
                          android:color="#44000000"
                          android:offset="0.0" />
                      <item
                          android:color="#00000000"
                          android:offset="1.0" />
                  </gradient>
              </aapt:attr>
          </path>
      </vector>
      

      gets reformatted to

      <vector
          xmlns:aapt="http://schemas.android.com/aapt"
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:height="108dp"
          android:viewportHeight="108"
          android:viewportWidth="108"
          android:width="108dp">
      
          <path android:pathData="L75.97,107.97L32,64Z">
      
              <attr name="android:fillColor">
      
                  <gradient
                      android:endX="78.5885"
                      android:type="linear">
      
                      <item
                          android:color="#44000000"
                          android:offset="0.0" />
      
                      <item
                          android:color="#00000000"
                          android:offset="1.0" />
                  </gradient>
              </attr>
          </path>
      </vector>
      
  • Fail gracefully on malformed XML

    Fail gracefully on malformed XML

    Exit with an error message instead of panicking.

    $ axmlfmt <(echo 'test')
    panic: runtime error: index out of range [-1]
    
    goroutine 1 [running]:
    github.com/rsookram/axmlfmt/internal/parse.ReadXML(0x5068c0, 0xc0000b8000, 0xc0000b8000, 0x0, 0xc0000a4018, 0x0, 0x0)
    	/path/to/axmlfmt/internal/parse/parse.go:63 +0xf02
    main.main()
    	/path/to/axmlfmt/cmd/axmlfmt/main.go:31 +0x142
    
Mdfmt - A Markdown formatter that follow the CommonMark. Like gofmt, but for Markdown

Introduction A Markdown formatter that follow the CommonMark. Like gofmt, but fo

Dec 18, 2022
Decode / encode XML to/from map[string]interface{} (or JSON); extract values with dot-notation paths and wildcards. Replaces x2j and j2x packages.

mxj - to/from maps, XML and JSON Decode/encode XML to/from map[string]interface{} (or JSON) values, and extract/modify values from maps by key or key-

Dec 29, 2022
omniparser: a native Golang ETL streaming parser and transform library for CSV, JSON, XML, EDI, text, etc.
omniparser: a native Golang ETL streaming parser and transform library for CSV, JSON, XML, EDI, text, etc.

omniparser Omniparser is a native Golang ETL parser that ingests input data of various formats (CSV, txt, fixed length/width, XML, EDI/X12/EDIFACT, JS

Jan 4, 2023
Convert xml and json to go struct

xj2go The goal is to convert xml or json file to go struct file. Usage Download and install it: $ go get -u -v github.com/wk30/xj2go/cmd/... $ xj [-t

Oct 23, 2022
parse and generate XML easily in go

etree The etree package is a lightweight, pure go package that expresses XML in the form of an element tree. Its design was inspired by the Python Ele

Dec 19, 2022
'go test' runner with output optimized for humans, JUnit XML for CI integration, and a summary of the test results.
'go test' runner with output optimized for humans, JUnit XML for CI integration, and a summary of the test results.

gotestsum gotestsum runs tests using go test --json, prints formatted test output, and a summary of the test run. It is designed to work well for both

Dec 28, 2022
xmlquery is Golang XPath package for XML query.

xmlquery Overview xmlquery is an XPath query package for XML documents, allowing you to extract data or evaluate from XML documents with an XPath expr

Jan 1, 2023
Go XML sitemap and sitemap index generator

Install go get github.com/turk/go-sitemap Example for sitemapindex func () main(c *gin.Context) { s := sitemap.NewSitemapIndex(c.Writer, true)

Jun 29, 2022
Extraction politique de conformité : xlsx (fichier de suivi) -> xml (format AlgoSec)

go_policyExtractor Extraction politique de conformité : xlsx (fichier de suivi) -> xml (format AlgoSec). Le programme suivant se base sur les intitulé

Nov 4, 2021
This package provides Go (golang) types and helper functions to do some basic but useful things with mxGraph diagrams in XML, which is most famously used by app.diagrams.net, the new name of draw.io.

Go Draw - Golang MX This package provides types and helper functions to do some basic but useful things with mxGraph diagrams in XML, which is most fa

Aug 30, 2022