Alex
Alex

Reputation: 3

How to edit XML file format using PHP script

I need to make a php script which re-format one xml file into another xml file

Whats the best way to go about this? simpleXML seems to be the solution but I am not able to extract the value from each tag and put it into the right formatted file.

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

//print_r($xml);

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

eg.

original 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>

after re-format xml

<?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>

Upvotes: 0

Views: 238

Answers (1)

iTollu
iTollu

Reputation: 1069

There are several possible ways to work with xml. Though you can use DOM and SAX approaches, the task of transforming xml is directly addressed by XSLT technology.

After you compose xsl-stylesheet, you can apply it to original xml either with php or with stand-alone xstl-processor like SAXON.

Using XSLT also benefits from IDE support. Such IDEs as NetBeans, IntellijIDEA/PHPStorm let have tools to comfortably compose and debug XSLT stylesheets.

Consider this XSLT, though it's maybe not ideal, because I haven't practiced XSLT for quite a long:

<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>

Upvotes: 1

Related Questions