我需要创建一个php脚本,将一个xml文件重新格式化为另一个xml文件
最好的办法是什么? simpleXML似乎是解决方案,但我无法从每个标记中提取值并将其放入正确的格式化文件中。
$xml = simplexml_load_file("file.xml");
//print_r($xml);
$array = json_decode(json_encode((array)$xml), TRUE);
例如
原始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="abc@abc.com"/>
<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后
<?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 & 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="abc@abc.com"/>
<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>
答案 0 :(得分:1)
有几种方法可以使用xml。虽然您可以使用DOM和SAX方法,但XSLT technology直接解决了转换xml的任务。
撰写xsl-stylesheet后,您可以将其应用于原始xml with php或使用独立的xstl处理器(如SAXON)。
使用XSLT也可以从IDE支持中受益。像NetBeans,IntellijIDEA / PHPStorm这样的IDE可以使用工具来轻松地编写和调试XSLT样式表。
考虑一下这个XSLT,虽然它可能并不理想,因为我已经很长时间没有实践过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>