2015-01-28 4 views
-1

PHP에 대해 많이 알지는 못하지만 저는 powershell로 변환하려는 PHP 스크립트를 상속 받았습니다. 이것이 가능한지 확실하지 않습니다. 그러나 스크립트가 서비스 목록에서 XML로 제품 목록을 얻는 것처럼 보입니다. 그런 다음 cr/lf 스트립 핑을 수행하고 몇 개의 따옴표 문자를 교환 한 다음 xml을 json으로 변환합니다. 꽤 똑바로 보인다. PowerShell에 대해서는 아직 잘 알지 못하기 때문에 여기에있는 사람에게는 이것이 간단한 작업이되기를 바랍니다. 여기 PHP 대 Powershell 스크립트

<?php 
    $fileContents= file_get_contents("http://svc.mysite.com/api/GetItems"); 

    $fileContents = str_replace(array("\n", "\r", "\t"), '', $fileContents); 

    $fileContents = trim(str_replace('"', "'", $fileContents)); 

    $simpleXml = simplexml_load_string($fileContents); 

    $map = json_decode(json_encode($simpleXml)); 

    print json_encode($map->item); 
?> 

내가이 함께했다

<?xml version="1.0" encoding="utf-8" ?> 
<items xmlns="http://svc.tricorbraun.com/api/GetItems" version="1"> 
    <item><ItemID>000072</ItemID><ItemName>Dropper Tip Closure</ItemName><ItemGroup>Closures</ItemGroup><Shape>Round</Shape><Style>Fitment/Plug</Style></item> 
    <item><ItemID>000182</ItemID><ItemName>28-480, P/P Push and Turn Closure</ItemName><ItemGroup>Closures</ItemGroup><Shape>Round</Shape><Style>Child Resistant</Style></item> 
    <item><ItemID>000187</ItemID><ItemName>28-480, P/P Child Resistant Closure</ItemName><ItemGroup>Closures</ItemGroup><Shape>Round</Shape><Style>Child Resistant</Style></item> 
    <item><ItemID>000190</ItemID><ItemName>28mm SPEC, P/P Dispensing Closure</ItemName><ItemGroup>Closures</ItemGroup><Shape>Round</Shape><Style>Dispensing</Style></item> 
</items> 

XML 데이터의 샘플이지만,

$doc = New-Object System.Xml.XmlDocument 
$memStream = new-object System.IO.MemoryStream 
$jsonWriter = [System.Runtime.Serialization.Json.JsonReaderWriterFactory]::CreateJsonWriter($memStream) 

$doc.Load("http://svc.mysite.com/api/GetItems") 
$doc.Save($jsonWriter) 
$bytes = $memStream.ToArray() 

[System.Text.Encoding]::UTF8.GetString($bytes,0,$bytes.Length) 
Write-Output $jsonWriter.ToString() 
+0

언어/시스템을 모르는 경우에는 Q & A 사이트가 아닌 프리랜서가 필요합니다. –

+0

나는 요구 사항을 만드는 사람이 아니다. 우리는 작업을 외주 처리하고이 PHP를 받았습니다. 우리는 명시 적으로 powershell을 요청했습니다. 그것이 더 복잡했다면, 나는 여기에 그것을 제출하지 않았을 것이다. 그러나 필요하다면 나는 스스로 알아낼 것이다. 무료 랜서가 필요하지 않아야합니다. –

+0

하청 업체와 같은 소리는 지정된 제품을 기침 할 때까지 발 구이가 필요합니다. – mjolinor

답변

1

에 '예기치 않은 네임 스페이스 오류'가 발생하면 우리는 무엇을 볼 필요가 회신은 다음 줄에서 이루어집니다.

$fileContents= file_get_contents("http://svc.mysite.com/api/GetItems"); 

Can y o PowerShell에서 다음을 실행하고 결과를 PasteBin 또는 다른 곳으로 던지십시오.

Invoke-WebRequest "http://svc.mysite.com/api/GetItems" | 
    Export-CLIXMl C:\temp\pathTo.XML 

당신이 일반적인 작업이 문자 제거하는 것을 완료하면 : "\ n을을", "\ 연구", 쉽고, 하나의 이중 따옴표를 대체하기 위해 "\의 t", 그것은 쉽습니다. PowerShell에는 ConvertFrom-Json 명령이있어이 기능도 쉽게 사용할 수 있습니다.

위에서 주어진 PowerShell 코드의 샘플 출력으로 답장을 보내면 나머지 작업을 도와 드릴 수 있습니다.


전체 파일 형식으로 답해 주셔서 감사합니다.

.xml 텍스트 파일에 .xml 출력이 있다고 가정하면이 프로세스는 모두 PowerShell로 JSON 개체를 제공합니다. 정말 간단합니다.

[xml]$string = get-content C:\temp\Pathto.xml 
$string.items.Item | Select-Object Item*,Shape,Style | ConvertTo-Json 

출력

[ 
{ 
    "ItemID": "000072", 
    "ItemName": "Dropper Tip Closure", 
    "ItemGroup": "Closures", 
    "Shape": "Round", 
    "Style": "Fitment/Plug" 
}, 
{ 
    "ItemID": "000182", 
    "ItemName": "28-480, P/P Push and Turn Closure", 
    "ItemGroup": "Closures", 
    "Shape": "Round", 
    "Style": "Child Resistant" 
}, 
{ 
    "ItemID": "000187", 
    "ItemName": "28-480, P/P Child Resistant Closure", 
    "ItemGroup": "Closures", 
    "Shape": "Round", 
    "Style": "Child Resistant" 
}, 
{ 
    "ItemID": "000190", 
    "ItemName": "28mm SPEC, P/P Dispensing Closure", 
    "ItemGroup": "Closures", 
    "Shape": "Round", 
    "Style": "Dispensing" 
} 
] 

나는 매일 JSON과 함께 작동하지만 유효한 JSON 봉투 및 내용 등의 확인 외모 나에게, 그것은 http://jsonlint.com/

에 대한 유효성을 검사하지 않습니다

다른 한 가지. 가장 먼저 첫 번째 줄을 대체 할 수 있습니다

[xml]$string = Invoke-WebRequest "http://svc.mysite.com/api/GetItems" 
+0

XML을 공유했습니다. –

+1

전체 convertto-Json 프로세스에 대한 다른 회신을 업데이트하고 있습니다. – FoxDeploy

+0

나는 그것이 나를 통해 일하기에 충분하다고 생각한다. 감사. –