XML Generator Tool
<# .SYNOPSIS XML Generator Tool .DESCRIPTION This script provides an interactive tool to create XML structures, add elements and attributes, and export the resulting XML to a file. .NOTES File Name : XMLGenerator.ps1 Author : [Your Name] Prerequisite : PowerShell V5.1 or later Version : 1.0 Date : [Current Date] .EXAMPLE .\XMLGenerator.ps1 #> # Global variables $script:xmlDoc = New-Object System.Xml.XmlDocument $script:currentElement = $null $script:xmlFilePath = "$env:USERPROFILE\Desktop\Generated_XML_$(Get-Date -Format 'yyyyMMdd_HHmmss').xml" function Show-Menu { Clear-Host Write-Host "=== XML Generator Tool ===" -ForegroundColor Cyan Write-Host "1. Create Root Element" Write-Host "2. Add Child Element" Write-Host "3. Add Attribute to Current Element" Write-Host "4. Add Text to Current Element" Write-Host "5. Move to Parent Element" Write-Host "6. View Current XML Structure" Write-Host "7. Export XML to File" Write-Host "8. Exit" } function Create-RootElement { $rootName = Read-Host "Enter the name for the root element" $root = $script:xmlDoc.CreateElement($rootName) $script:xmlDoc.AppendChild($root) | Out-Null $script:currentElement = $root Write-Host "Root element '$rootName' created." -ForegroundColor Green } function Add-ChildElement { if ($null -eq $script:currentElement) { Write-Host "No current element selected. Please create a root element first." -ForegroundColor Yellow return } $childName = Read-Host "Enter the name for the child element" $child = $script:xmlDoc.CreateElement($childName) $script:currentElement.AppendChild($child) | Out-Null $script:currentElement = $child Write-Host "Child element '$childName' added to current element." -ForegroundColor Green } function Add-Attribute { if ($null -eq $script:currentElement) { Write-Host "No current element selected. Please create an element first." -ForegroundColor Yellow return } $attrName = Read-Host "Enter the name of the attribute" $attrValue = Read-Host "Enter the value of the attribute" $script:currentElement.SetAttribute($attrName, $attrValue) Write-Host "Attribute '$attrName' added to current element." -ForegroundColor Green } function Add-Text { if ($null -eq $script:currentElement) { Write-Host "No current element selected. Please create an element first." -ForegroundColor Yellow return } $text = Read-Host "Enter the text content for the current element" $script:currentElement.InnerText = $text Write-Host "Text added to current element." -ForegroundColor Green } function Move-ToParentElement { if ($null -eq $script:currentElement -or $script:currentElement -eq $script:xmlDoc.DocumentElement) { Write-Host "Already at the root level or no element selected." -ForegroundColor Yellow return } $script:currentElement = $script:currentElement.ParentNode Write-Host "Moved to parent element." -ForegroundColor Green } function View-CurrentXML { if ($null -eq $script:xmlDoc.DocumentElement) { Write-Host "XML structure is empty. Please create a root element first." -ForegroundColor Yellow return } $xmlString = $script:xmlDoc.OuterXml $xmlFormatted = Format-XML $xmlString Write-Host "Current XML Structure:" -ForegroundColor Yellow Write-Host $xmlFormatted } function Format-XML([string]$xmlString) { $stringWriter = New-Object System.IO.StringWriter $xmlWriter = New-Object System.Xml.XmlTextWriter($stringWriter) $xmlWriter.Formatting = [System.Xml.Formatting]::Indented $xmlDoc = New-Object System.Xml.XmlDocument $xmlDoc.LoadXml($xmlString) $xmlDoc.WriteContentTo($xmlWriter) $xmlWriter.Flush() $stringWriter.Flush() return $stringWriter.ToString() } function Export-XMLToFile { if ($null -eq $script:xmlDoc.DocumentElement) { Write-Host "XML structure is empty. Please create a root element first." -ForegroundColor Yellow return } try { $script:xmlDoc.Save($script:xmlFilePath) Write-Host "XML exported successfully to: $script:xmlFilePath" -ForegroundColor Green } catch { Write-Host "Error exporting XML: $_" -ForegroundColor Red } } # Main program loop do { Show-Menu $choice = Read-Host "`nEnter your choice (1-8)" switch ($choice) { "1" { Create-RootElement } "2" { Add-ChildElement } "3" { Add-Attribute } "4" { Add-Text } "5" { Move-ToParentElement } "6" { View-CurrentXML } "7" { Export-XMLToFile } "8" { Write-Host "Exiting program..." -ForegroundColor Yellow; break } default { Write-Host "Invalid choice. Please try again." -ForegroundColor Red } } if ($choice -ne "8") { Read-Host "`nPress Enter to continue..." } } while ($choice -ne "8")
This XML Generator Tool includes:
- A menu-driven interface for easy navigation.
- Functions to interactively build XML structures:
- Create a root element
- Add child elements
- Add attributes to elements
- Add text content to elements
- Navigate the XML structure (move to parent element)
- Ability to view the current XML structure at any time.
- Option to export the generated XML to a file.
Key features:
- Interactive XML creation process
- Hierarchical element creation and navigation
- Support for adding attributes and text content
- Real-time viewing of the current XML structure
- XML formatting for better readability
- Export functionality to save the generated XML
This tool is particularly useful for:
- Developers who need to create XML structures for testing or configuration purposes
- Anyone learning about XML structure and wanting to experiment with creating XML documents
- System administrators who need to generate XML files for various applications
- Quality Assurance professionals creating XML test data
To use this script effectively:
- Run the script in PowerShell
- Use the menu options to build your XML structure:
- Start by creating a root element
- Add child elements, attributes, and text as needed
- Use the “Move to Parent Element” option to navigate back up the XML tree
- View the current XML structure at any time to check your progress
- When finished, export the XML to a file
This script provides a user-friendly way to create XML structures without having to manually write XML syntax. It’s especially helpful for those who are new to XML or need to quickly generate XML files without writing them by hand.
Leave a Reply
Want to join the discussion?Feel free to contribute!