2014-11-20 2 views
0

하나의 XML 파일을 다른 XML 파일로 다시 포맷하는 PHP 스크립트를 만들어야합니다.PHP 스크립트를 사용하여 XML 파일 형식을 편집하는 방법

어떻게해야할까요? simpleXML이 해결책 인 것 같지만 각 태그에서 값을 추출하여 올바른 형식의 파일에 넣을 수는 없습니다.

$xml = simplexml_load_file("file.xml"); 

//print_r($xml); 

$array = json_decode(json_encode((array)$xml), TRUE); 

예 :

다시 포맷 XML 후 원래의 XML은

<?xml version="1.0" encoding="ISO-8859-1"?> 
<ADI> 
    <Metadata> 
     <AMS Provider="" Product="MOD" Asset_Name="SpecialAgentOso1-03AViewToABook/Diamond'sAreForKitesPackageAsset" Version_Major="1" Version_Minor="0" Description="Special Agent Oso" Creation_Date="2014-10-20" Provider_ID="" Asset_ID="10010000000019545827" Asset_Class="package"/> 
     <App_Data App="MOD" Name="Metadata_Spec_Version" Value="CableLabsVOD1.1"/> 
    </Metadata> 
    <Asset> 
     <Metadata> 
      <AMS Provider="" Product="MOD" Asset_Name="SpecialAgentOso1-03AViewToABook/Diamond'sAreForKitesTitleAsset" Version_Major="1" Version_Minor="0" Description="Special Agent Oso" Creation_Date="2014-10-20" Provider_ID="" Asset_ID="10010100000019545827" Asset_Class="title"/> 
      <App_Data App="MOD" Name="Type" Value="title"/> 
      <App_Data App="MOD" Name="Title_Sort_Name" Value="Special Agent Oso"/> 
      <App_Data App="MOD" Name="Title_Brief" Value="SpecialAgentOSO1-03"/> 
      <App_Data App="MOD" Name="Title" Value="Special Agent Oso 1-03 A View To A Book/ Diamond's Are For Kites"/> 
      <App_Data App="MOD" Name="Episode_Name" Value="A View To A Book/ Diamond's Are For Kites"/> 
      <App_Data App="MOD" Name="Episode_ID" Value="Episode#03"/> 
      <App_Data App="MOD" Name="Summary_Medium" Value="When Joe needs a book to identify the toy dinosaurs he's taking to school, Oso borrows a book from the library. /When Sarah wants to be a part of Kite Day but isn't sure how to fly a kite, Oso helps Sarah find a kite and get it in the air."/> 
      <App_Data App="MOD" Name="Summary_Long" Value="When Joe needs a book to identify the toy dinosaurs he's taking to school, Oso borrows a book from the library. /When Sarah wants to be a part of Kite Day but isn't sure how to fly a kite, Oso helps Sarah find a kite and get it in the air."/> 
      <App_Data App="MOD" Name="Summary_Short" Value="When Joe needs a book to identify the toy dinosaurs he's taking to school, Oso borrows a book from the library. /When Sarah wants to be a part of Kite Day but isn't sure how to fly a kite, Oso helps Sarah find a kite and get it in the air."/> 
      <App_Data App="MOD" Name="Rating" Value="TV-Y"/> 
      <App_Data App="MOD" Name="Closed_Captioning" Value="Y"/> 
      <App_Data App="MOD" Name="Run_Time" Value="00:24:04"/> 
      <App_Data App="MOD" Name="Display_Run_Time" Value="00:24"/> 
      <App_Data App="MOD" Name="Year" Value="2008"/> 
      <App_Data App="MOD" Name="Country_of_Origin" Value="US"/> 
      <App_Data App="MOD" Name="Studio" Value="Disney"/> 
      <App_Data App="MOD" Name="Category" Value="Disney Junior On Demand/Special Agent OSO"/> 
      <App_Data App="MOD" Name="Genre" Value="Adventure"/> 
      <App_Data App="MOD" Name="Genre" Value="Mystery"/> 
      <App_Data App="MOD" Name="Genre" Value="Animation"/> 
      <App_Data App="MOD" Name="Box_Office" Value="0"/> 
      <App_Data App="MOD" Name="Billing_ID" Value="1003"/> 
      <App_Data App="MOD" Name="Licensing_Window_Start" Value="2014-11-07T00:00:00"/> 
      <App_Data App="MOD" Name="Licensing_Window_End" Value="2014-11-20T23:59:59"/> 
      <App_Data App="MOD" Name="Preview_Period" Value="0"/> 
      <App_Data App="MOD" Name="Provider_QA_Contact" Value="[email protected]"/> 
      <App_Data App="MOD" Name="Suggested_Price" Value="0.00"/> 
     </Metadata> 
     <Asset> 
      <Metadata> 
       <AMS Provider="" Product="MOD" Asset_Name="SpecialAgentOso1-03AViewToABook/Diamond'sAreForKitesMovieAsset" Version_Major="1" Version_Minor="0" Description="Special Agent Oso" Creation_Date="2014-10-20" Provider_ID="" Asset_ID="10010200000019545827" Asset_Class="movie"/> 
       <App_Data App="MOD" Name="Type" Value="movie"/> 
       <App_Data App="MOD" Name="Audio_Type" Value="Stereo"/> 
       <App_Data App="MOD" Name="HDContent" Value="N"/> 
       <App_Data App="MOD" Name="Viewing_Can_Be_Resumed" Value="N"/> 
       <App_Data App="MOD" Name="Watermarking" Value="N"/> 
       <App_Data App="MOD" Name="Copy_Protection" Value="N"/> 
       <App_Data App="MOD" Name="Content_FileSize" Value=""/> 
       <App_Data App="MOD" Name="Content_CheckSum" Value=""/> 
      </Metadata> 
      <Content Value="FOD31299.mpg"/> 
     </Asset> 
    </Asset> 
</ADI> 

<?xml version="1.0"> 
<!DOCTYPE ADI SYSTEM "ADI.DTD"> 
<ADI> 
    <Metadata> 
    <AMS Provider="FAMILY" Product="MOD" Asset_Name="SpecialAgentOso1-03AViewToABook/Diamond'sAreForKitesPackageAsset" Version_Major="1" Version_Minor="0" Description="Special Agent Oso" Creation_Date="2014-10-20" Provider_ID="astral.com" Asset_ID="10010000000019545827" Asset_Class="PACKAGE"/> 
    <App_Data App="MOD" Name="Metadata_Spec_Version" Value="CableLabsVOD1.1"/> 
    </Metadata> 
    <Asset> 
    <Metadata> 
     <AMS Provider="FAMILY" Product="MOD" Asset_Name="SpecialAgentOso1-03AViewToABook/Diamond'sAreForKitesTitleAsset" Version_Major="1" Version_Minor="0" Description="Special Agent Oso" Creation_Date="2014-10-20" Provider_ID="astral.com" Asset_ID="10010100000019545827" Asset_Class="TITLE"/> 
     <App_Data App="MOD" Name="Type" Value="title"/> 
     <App_Data App="MOD" Name="Title_Brief" Value="SpecialAgentOSO1-03"/> 
     <App_Data App="MOD" Name="Title" Value="SpecialAgentOSO1-03"/> 
     <App_Data App="MOD" Name="Show_Type" Value="Series"/> 
     <App_Data App="MOD" Name="Episode_Name" Value="A View To A Book/ Diamond's Are For Kites"/> 
     <App_Data App="MOD" Name="Episode_ID" Value="03"/> 
     <App_Data App="MOD" Name="Series_Name" Value="Special Agent Oso"/> 
     <App_Data App="MOD" Name="Season_Number" Value="1"/> 
     <App_Data App="MOD" Name="Summary_Long" Value="When Joe needs a book to identify the toy dinosaurs he's taking to school, Oso borrows a book from the library. /When Sarah wants to be a part of Kite Day but isn't sure how to fly a kite, Oso helps Sarah find a kite and get it in the air."/> 
     <App_Data App="MOD" Name="Summary_Long" Value="When Joe needs a book to identify the toy dinosaurs he's taking to school, Oso borrows a book from the library. /When Sarah wants to be a part of Kite Day but isn't sure how to fly a kite, Oso helps Sarah find a kite and get it in the air."/> 
     <App_Data App="MOD" Name="Rating" Value="G"/> 
     <App_Data App="MOD" Name="Closed_Captioning" Value="Y"/> 
     <App_Data App="MOD" Name="Run_Time" Value="00:24:04"/> 
     <App_Data App="MOD" Name="Display_Run_Time" Value="00:24"/> 
     <App_Data App="MOD" Name="Year" Value="2008"/> 
     <App_Data App="MOD" Name="Actors" Value="N/A"/> 
     <App_Data App="MOD" Name="Director" Value="N/A"/> 
     <App_Data App="MOD" Name="Canadian_Content" Value="0"/> 
     <App_Data App="MOD" Name="Country_Of_Origin" Value="USA"/> 
     <App_Data App="MOD" Name="Category" Value="Subscription/Family &amp; Kids/Disney Junior On Demand/Special Agent OSO"/> 
     <App_Data App="MOD" Name="Genre" Value="Series/All"/> 
     <App_Data App="MOD" Name="Genre" Value="Children"/> 
     <App_Data App="MOD" Name="Billing_ID" Value="1003"/> 
     <App_Data App="MOD" Name="Licensing_Window_Start" Value="2014-11-07T00:00:00"/> 
     <App_Data App="MOD" Name="Licensing_Window_End" Value="2014-11-20T23:59:59"/> 
     <App_Data App="MOD" Name="Preview_Period" Value="0"/> 
     <App_Data App="MOD" Name="Maximum_Viewing_Length" Value="1440"/> 
     <App_Data App="MOD" Name="Provider_QA_Contact" Value="[email protected]"/> 
     <App_Data App="MOD" Name="Suggested_Price" Value="0.00"/> 
    </Metadata> 
    <Asset> 
     <Metadata> 
     <AMS Provider="FAMILY" Product="MOD" Asset_Name="SpecialAgentOSO1-03MetadataMovieAsset" Version_Major="1" Version_Minor="0" Description="SpecialAgentOSO1-03 Metadata Movie Asset" Creation_Date="2014-10-20" Provider_ID="astral.com" Asset_ID="10010200000019545827" Asset_Class="MOVIE"/> 
     <App_Data App="MOD" Name="Encryption" Value="Y"/> 
     <App_Data App="MOD" Name="Type" Value="movie"/> 
     <App_Data App="MOD" Name="Audio_Type" Value="stereo"/> 
     <App_Data App="MOD" Name="HDContent" Value="N"/> 
     <App_Data App="MOD" Name="Copy_Protection" Value="Y"/> 
     <App_Data App="MOD" Name="Languages" Value="en"/> 
     </Metadata> 
     <Content Value="FOD31299.mpg"/> 
    </Asset> 
    <Asset> 
     <Metadata> 
     <AMS Provider="FAMILY" Product="MOD" Asset_Name="SpecialAgentOSO1-03MetadataPreviewAsset" Version_Major="1" Version_Minor="0" Description="SpecialAgentOSO1-03 Metadata Preview Asset" Creation_Date="2014-10-20" Provider_ID="astral.com" Asset_ID="10010300000019545827" Asset_Class="PREVIEW"/> 
     <App_Data App="MOD" Name="Type" Value="preview"/> 
     </Metadata> 
     <Content Value="FOD31299_preview.mpg"/> 
    </Asset> 
    <Asset> 
     <Metadata> 
     <AMS Provider="FAMILY" Product="MOD" Asset_Name="SpecialAgentOSO1-03MetadataPosterAsset" Version_Major="1" Version_Minor="0" Description="SpecialAgentOSO1-03 Metadata Poster Asset" Creation_Date="2014-10-20" Provider_ID="astral.com" Asset_ID="10010400000019545827" Asset_Class="POSTER"/> 
     <App_Data App="MOD" Name="Type" Value="poster"/> 
     </Metadata> 
     <Content Value="FOD31299_poster.jpg"/> 
    </Asset> 
    </Asset> 
</ADI> 

답변

1

는 XML을하려면 여러 가지 방법이 있습니다. DOM 및 SAX 접근법을 사용할 수 있지만 xml 변환 작업은 XSLT technology에 의해 직접 처리됩니다.

xsl-stylesheet를 작성한 후 with php 또는 SAXON과 같은 독립 실행 형 xstl 프로세서로 원본 xml에 적용 할 수 있습니다.

XSLT를 사용하면 IDE 지원도 가능합니다. NetBeans, IntellijIDEA/PHPStorm 같은 IDE는 XSLT 스타일 시트를 편안하게 작성하고 디버깅 할 수있는 도구를 제공합니다.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" indent="yes" doctype-system="ADI.DTD"/> 

    <xsl:template match="/"> 
     <xsl:element name="ADI"> 
      <xsl:apply-templates select="*"/> 
     </xsl:element> 
    </xsl:template> 

    <xsl:template match="Metadata"> 
     <xsl:element name="Metadata"> 
      <xsl:apply-templates select="AMS"/> 
      <xsl:for-each select="App_Data"> 
       <xsl:copy-of select="."/> 
      </xsl:for-each> 
     </xsl:element> 
    </xsl:template> 

    <xsl:template match="Asset"> 
     <xsl:element name="Asset"> 
      <xsl:apply-templates/> 
     </xsl:element> 
    </xsl:template> 

    <xsl:template match="Content"> 
     <xsl:copy-of select="."/> 
    </xsl:template> 

    <xsl:template match="AMS"> 
     <xsl:element name="AMS"> 
      <xsl:apply-templates select="@*"/> 
     </xsl:element> 
    </xsl:template> 


    <xsl:template match="@Provider"> 
     <xsl:attribute name="Provider"> 
      <xsl:text>FAMILY</xsl:text> 
     </xsl:attribute> 
    </xsl:template> 

    <xsl:template match="@Provider_ID"> 
     <xsl:attribute name="Provider_ID"> 
      <xsl:text>astral.com</xsl:text> 
     </xsl:attribute> 
    </xsl:template> 

    <xsl:template 
      match="@Product|@Asset_Name|@Version_Major|@Version_Minor|@Description|@Creation_Date|@Asset_ID|@Asset_Class"> 
     <xsl:copy-of select="."/> 
    </xsl:template> 

</xsl:stylesheet> 
+0

내가 새 XML 파일을 생성하기 위해이 같은 일을 실행할 수 XSL에 도움 을 감사합니다 : 그것은 어쩌면 좋지 않은 비록 내가 꽤 오랫동안 XSLT 연습을하지 않았기 때문에

,이 XSLT를 고려? load ('file.xml'); $ xsl = 새 DOMDocument; $ xsl-> load ('style.xsl'); // 변압기 구성 $ proc = new XSLTProcessor; $ proc-> importStyleSheet ($ xsl); // xsl 규칙을 첨부하십시오. echo $ proc-> transformToXML ($ xml); ?> – Alex

+0

1. 원본 XML을 DOMDocument ($ xml)에로드하십시오. 2. xslt를 DOMDocument ($ xsltStylesheet)에로드합니다. 3. XSLTProcessor ($ xsltProcessor)의 새 인스턴스를 만듭니다. 4. $ xsltProcessor-> importStylesheet ($ xsltStylesheet);로로드 된 xslt를 가져옵니다. 5. $ xsltProcessor-> transformToXML ($ xml)을 사용하여 변환 된 XML을 가져옵니다. 출력으로 변환 된 XML을 포함하는 문자열을 얻어야합니다. – iTollu