Skip to content

Adhidevudayakumar/react-native-to-xml-converter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

React Native to XML Converter

A powerful CLI tool that converts React Native components to Android XML layouts. This tool parses your React Native JSX/TSX files and generates corresponding Android XML layout files, making it easy to port React Native components to native Android.

Features

  • πŸ”„ Automatic Conversion: Converts React Native components to Android XML
  • 🎨 Style Mapping: Maps React Native styles to Android attributes
  • πŸ“¦ Component Support: Supports common RN components (View, Text, ScrollView, etc.)
  • πŸš€ CLI Tool: Easy-to-use command-line interface
  • πŸ“ Organized Output: Generates XML files in a dedicated xml/ folder
  • ✨ TypeScript Support: Written in TypeScript with full type definitions

Installation

Global Installation`

npm install -g react-native-to-xml-converter

Local Installation

npm install --save-dev react-native-to-xml-converter

Using npx (No Installation Required)

npx react-native-to-xml-converter <input-file>

Usage

Basic Usage

# Using installed package
rn-to-xml example.ts

# Using npx
npx rn-to-xml example.ts

The converted XML file will be saved in the xml/ folder in your current directory.

Example

Input (example.ts):

import { View, Text } from 'react-native';

export default function Example() {
  return (
    <View style={{ padding: 20, backgroundColor: '#f0f0f0' }}>
      <Text style={{ fontSize: 18, color: '#333' }}>Hello World</Text>
      <View style={{ marginTop: 10 }}>
        <Text>This is a test component</Text>
      </View>
    </View>
  );
}

Output (xml/example.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/view_1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="#f0f0f0"
    android:padding="20dp">
    <TextView
        android:id="@+id/text_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World"
        android:textColor="#333"
        android:textSize="18sp"/>
    <LinearLayout
        android:id="@+id/view_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginTop="10dp">
        <TextView
            android:id="@+id/text_4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="This is a test component"/>
    </LinearLayout>
</LinearLayout>

Supported Components

React Native Android XML
View LinearLayout
Text TextView
Image ImageView
ScrollView ScrollView
TextInput EditText
Button Button
TouchableOpacity FrameLayout
FlatList RecyclerView

Supported Styles

Layout Properties

  • padding, paddingTop, paddingBottom, paddingLeft, paddingRight
  • paddingHorizontal, paddingVertical
  • margin, marginTop, marginBottom, marginLeft, marginRight
  • marginHorizontal, marginVertical
  • width, height

View Properties

  • backgroundColor
  • flexDirection (determines layout orientation)
  • alignItems, justifyContent (converted to gravity)

Text Properties

  • fontSize
  • color
  • fontWeight
  • textAlign

Programmatic API

You can also use this package programmatically in your Node.js scripts:

import { convertRNToXML } from 'react-native-to-xml-converter';

const code = `
import { View, Text } from 'react-native';

export default function App() {
  return (
    <View style={{ padding: 20 }}>
      <Text>Hello World</Text>
    </View>
  );
}
`;

const xml = convertRNToXML(code);
console.log(xml);

Development

Building from Source

# Clone the repository
git clone https://github.com/Adhidevudayakumar/react-native-to-xml-converter.git
cd react-native-to-xml-converter

# Install dependencies
npm install

# Build the project
npm run build

# Link locally for testing
npm link

# Test the CLI
npx rn-to-xml example.ts

Project Structure

β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ cli/           # CLI entry point
β”‚   β”œβ”€β”€ parser/        # AST parsing logic
β”‚   β”œβ”€β”€ transformer/   # Component and style mappers
β”‚   β”œβ”€β”€ generators/    # XML generation
β”‚   β”œβ”€β”€ utils/         # Helper utilities
β”‚   └── index.ts       # Public API
β”œβ”€β”€ dist/              # Compiled output
β”œβ”€β”€ xml/               # Generated XML files (created on first run)
└── package.json

Limitations

  • Only supports inline styles (not StyleSheet.create references yet)
  • Some complex React Native components may not have direct Android equivalents
  • Nested Text components are flattened to single TextView
  • Custom components are treated as View by default

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

ISC

Author

Adhidev Udayakumar

Links

About

A powerful CLI tool that converts React Native components to Android XML layouts. This tool parses your React Native JSX/TSX files and generates corresponding Android XML layout files, making it easy to port React Native components to native Android.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors