การแปลงไฟล์ XML เป็นรูปแบบ JSON เฉพาะ
Oct 21 2020
ฉันมีไฟล์ XML ที่มีลักษณะเช่นนี้
<?xml version="1.0" encoding="utf-8"?>
<Application xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Name="application" xmlns="http://schemas.microsoft.com/2011/01/fabric">
<Parameters>
<Parameter Name="Parameter1" Value="test1" />
<Parameter Name="parameter2" Value="test2" />
<Parameter Name="parameter3" Value="test3" />
<Parameter Name="parameter4" Value="test4" />
</Parameters>
</Application>
ฉันใช้แฮชแท็กเพื่อแปลงสิ่งนี้เป็น JSON แต่เมื่อฉันทำฉันจะได้รับเฉพาะส่วนพารามิเตอร์:
{
"PARAMETER_1": "Test",
"parameter2" : "test2"
ฉันกำลังอ่านไฟล์ xml เช่นนั้นเพราะถ้าฉันพยายามอ่านเป็น $ appParametersXml ฉันจะได้รับข้อผิดพลาด
$appParametersHashTable = Read-XmlElementAsHashtable $appParametersXml.Application.Parameters $appParametersHashTable |ConvertTo-Json |Out-File "C:\Workspaces\ARMTemplates\GhostDraftApplication\ARMTemplate\CloudAXF.json"
#$appParametersHashTable|ConvertFrom-Xml|Out-File "C:\Workspaces\ARMTemplates\GhostDraftApplication\ARMTemplate\CloudTest.xml" $appParametersXml | ConvertFrom-Xml | ConvertTo-Json -Depth 4 | Out-File "C:\Workspaces\ARMTemplates\GhostDraftApplication\ARMTemplate\CloudAXF.json"
$xmlObject = $appParametersXml | ConvertTo-Xml
###############################################################################
function Read-XmlElementAsHashtable
{
Param (
[System.Xml.XmlElement]
$Element ) $hashtable = @{}
if ($Element.ChildNodes) { $Element.ChildNodes |
ForEach-Object {
$hashtable["Name" + $_.Name] = "Value" + $_.Value } } return $hashtable
}
มีวิธีอ่านแท็กและแอตทริบิวต์ทั้งหมดจากไฟล์ xml และแปลงเป็น JSON หรือไม่?
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"PARAMETER_1": {
"value": "Test"
},
ฉันเพิ่งหมุนวงล้อ
คำตอบ
1 mklement0 Oct 21 2020 at 20:23
หากคุณสามารถฮาร์ดโค้ดได้ว่าจะดึงแอตทริบิวต์ใดจากParameterองค์ประกอบ XML คุณสามารถทำสิ่งต่อไปนี้:
# Sample XML input.
[xml] $xmlDoc = @' <?xml version="1.0" encoding="utf-8"?> <Application xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Name="application" xmlns="http://schemas.microsoft.com/2011/01/fabric"> <Parameters> <Parameter Name="Parameter1" Value="test1" /> <Parameter Name="parameter2" Value="test2" /> <Parameter Name="parameter3" Value="test3" /> <Parameter Name="parameter4" Value="test4" /> </Parameters> </Application> '@ # Transform the "Parameter" elements into a nested hashtable. $hash = [ordered] @{}
$xmlDoc.Application.Parameters.ChildNodes | % { $hash[$_.Name] = @{ value = $_.Value }
}
# Wrap the hashtable in a top-level hashtable and convert to JSON.
[ordered] @{
'$schema' = 'https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#' contentVersion ='1.0.0.0' parameters = $hash
} | ConvertTo-Json
ผลตอบแทนข้างต้น:
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"Parameter1": {
"value": "test1"
},
"parameter2": {
"value": "test2"
},
"parameter3": {
"value": "test3"
},
"parameter4": {
"value": "test4"
}
}
}
หมายเหตุ: หากคุณต้องการแปลงประเภทเพิ่มเติมในอินพุต XML ก่อนที่จะแปลงเป็น JSON โปรดดูคำตอบสำหรับคำถามที่เกี่ยวข้อง