PHP - Parsing invalid XML and outputting to own xml document -
trying figure out effective way parse invalid xml (in php).
the element i'm attempting parse contains nested that'd output own xml doc.
i've tried simple xml, continuously running errors. tried using dom , seems work well, resulted in being able output text. appreciated.
sample xml:
<?xml version="1.0" encoding="utf-8"?> <server_name> <creative> <content><vast xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" version="2.0" xsi:nonamespaceschemalocation="vast.xsd"> <ad id="222296909"> <inline> <adsystem version="2.0">dart_dfa</adsystem> <adtitle>in-stream video</adtitle> <description>parisian love - updated</description> <survey/> <impression id="dart"> <![cdata[ http://ad.doubleclick.net/imp;v7;x;222296909;0-0;0;45922216;0/0;35460632/35478450/2;;~cs=s%3fhttp://s0.2mdn.net/dot.gif ]]> </impression> <creatives> <creative sequence="1" adid=""> <linear> <duration>00:00:52</duration> <trackingevents> <tracking event="start"> <![cdata[ http://ad.doubleclick.net/activity;src=1379578;met=1;v=1;pid=45922216;aid=222296909;ko=0;cid=35460632;rid=35478450;rv=2;timestamp=1242003;eid1=11;ecn1=1;etm1=0; ]]> </tracking> <tracking event="midpoint"> <![cdata[ http://ad.doubleclick.net/activity;src=1379578;met=1;v=1;pid=45922216;aid=222296909;ko=0;cid=35460632;rid=35478450;rv=2;timestamp=1242003;eid1=18;ecn1=1;etm1=0; ]]> </tracking> <tracking event="complete"> <![cdata[ http://ad.doubleclick.net/activity;src=1379578;met=1;v=1;pid=45922216;aid=222296909;ko=0;cid=35460632;rid=35478450;rv=2;timestamp=1242003;eid1=13;ecn1=1;etm1=0; ]]> </tracking> <tracking event="mute"> <![cdata[ http://ad.doubleclick.net/activity;src=1379578;met=1;v=1;pid=45922216;aid=222296909;ko=0;cid=35460632;rid=35478450;rv=2;timestamp=1242003;eid1=16;ecn1=1;etm1=0; ]]> </tracking> <tracking event="pause"> <![cdata[ http://ad.doubleclick.net/activity;src=1379578;met=1;v=1;pid=45922216;aid=222296909;ko=0;cid=35460632;rid=35478450;rv=2;timestamp=1242003;eid1=15;ecn1=1;etm1=0; ]]> </tracking> <tracking event="fullscreen"> <![cdata[ http://ad.doubleclick.net/activity;src=1379578;met=1;v=1;pid=45922216;aid=222296909;ko=0;cid=35460632;rid=35478450;rv=2;timestamp=1242003;eid1=19;ecn1=1;etm1=0; ]]> </tracking> </trackingevents> <adparameters/> <videoclicks> <clickthrough> <![cdata[ http://google.com ]]> </clickthrough> <clicktracking id="dart"> <![cdata[ http://ad.doubleclick.net/click%3bh%3dv8/3fae/3/0/%2a/k%3b222296909%3b0-0%3b0%3b45922216%3b255-0/0%3b35460632/35478450/2%3b%3b%7esscs%3d%3fhttp://s0.2mdn.net/dot.gif ]]> </clicktracking> </videoclicks> <mediafiles> <mediafile id="1" delivery="streaming" type="video/x-flv" bitrate="766" width="854" height="480"> <![cdata[ rtmp://rmcdn.f.2mdn.net/ondemand/motiffiles/html/1379578/parisian_love_126566284014011.flv ]]> </mediafile> <mediafile id="2" delivery="progressive" type="video/x-flv" bitrate="766" width="854" height="480"> <![cdata[ http://gcdn.2mdn.net/motiffiles/html/1379578/parisian_love_126566284014011.flv ]]> </mediafile> </mediafiles> </linear> </creative> <creative sequence="1" adid=""> <companionads> <companion id="1" width="300" height="250"> <staticresource creativetype="application/x-shockwave-flash"> <![cdata[ http://s0.2mdn.net/1379578/pid_1244559_1265644799000_parisian_love_300x250.swf ]]> </staticresource> <companionclickthrough> <![cdata[ http://google.com ]]> </companionclickthrough> <alttext/> <adparameters/> </companion> <companion id="2" width="300" height="250"> <staticresource creativetype="image/jpeg"> <![cdata[ http://s0.2mdn.net/1379578/pid_1244559_1265644904000_parisian_love_300x250.jpg ]]> </staticresource> <companionclickthrough> <![cdata[ http://google.com ]]> </companionclickthrough> <alttext/> <adparameters/> </companion> <companion id="3" width="728" height="90"> <staticresource creativetype="image/jpeg"> <![cdata[ http://s0.2mdn.net/1379578/pid_1244559_1265644932000_parisian_love_728x90.jpg ]]> </staticresource> <companionclickthrough> <![cdata[ http://google.com ]]> </companionclickthrough> <alttext/> <adparameters/> </companion> <companion id="4" width="728" height="90"> <staticresource creativetype="application/x-shockwave-flash"> <![cdata[ http://s0.2mdn.net/1379578/pid_1244559_1265644945000_parisian_love_728x90.swf ]]> </staticresource> <companionclickthrough> <![cdata[ http://google.com ]]> </companionclickthrough> <alttext/> <adparameters/> </companion> </companionads> </creative> <creative sequence="2" adid=""> <nonlinearads> <trackingevents> <tracking event="start"> <![cdata[ http://ad.doubleclick.net/activity;src=1379578;met=1;v=1;pid=45922216;aid=222296909;ko=0;cid=35460632;rid=35478450;rv=2;timestamp=1242003;eid1=11;ecn1=1;etm1=0; ]]> </tracking> <tracking event="midpoint"> <![cdata[ http://ad.doubleclick.net/activity;src=1379578;met=1;v=1;pid=45922216;aid=222296909;ko=0;cid=35460632;rid=35478450;rv=2;timestamp=1242003;eid1=18;ecn1=1;etm1=0; ]]> </tracking> <tracking event="complete"> <![cdata[ http://ad.doubleclick.net/activity;src=1379578;met=1;v=1;pid=45922216;aid=222296909;ko=0;cid=35460632;rid=35478450;rv=2;timestamp=1242003;eid1=13;ecn1=1;etm1=0; ]]> </tracking> <tracking event="mute"> <![cdata[ http://ad.doubleclick.net/activity;src=1379578;met=1;v=1;pid=45922216;aid=222296909;ko=0;cid=35460632;rid=35478450;rv=2;timestamp=1242003;eid1=16;ecn1=1;etm1=0; ]]> </tracking> <tracking event="pause"> <![cdata[ http://ad.doubleclick.net/activity;src=1379578;met=1;v=1;pid=45922216;aid=222296909;ko=0;cid=35460632;rid=35478450;rv=2;timestamp=1242003;eid1=15;ecn1=1;etm1=0; ]]> </tracking> <tracking event="fullscreen"> <![cdata[ http://ad.doubleclick.net/activity;src=1379578;met=1;v=1;pid=45922216;aid=222296909;ko=0;cid=35460632;rid=35478450;rv=2;timestamp=1242003;eid1=19;ecn1=1;etm1=0; ]]> </tracking> </trackingevents> <nonlinear id="1" width="400" height="150" scalable="false" maintainaspectratio="false"> <staticresource creativetype="image/jpeg"> <![cdata[ http://s0.2mdn.net/1379578/pid_1244559_1281625520000_google.jpg ]]> </staticresource> <nonlinearclickthrough> <![cdata[ http://google.com ]]> </nonlinearclickthrough> <adparameters/> </nonlinear> </nonlinearads> </creative> </creatives> <extensions> <extension type="dart"> <adservingdata> <deliverydata> <geodata> <![cdata[ ct=us&st=ny&ac=212&zp=10025&bw=3&dma=3&city=13275 ]]> </geodata> </deliverydata> </adservingdata> </extension> </extensions> </inline> </ad> </vast></content> </creative> </server_name>
output contained within element , like:
<?xml version="1.0" encoding="utf-8"?> <vast xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" version="2.0" xsi:nonamespaceschemalocation="vast.xsd"> <ad id="222296909"> <inline> <adsystem version="2.0">dart_dfa</adsystem> <adtitle>in-stream video</adtitle> <description>parisian love - updated</description> <survey /> <impression id="dart"><![cdata[http://ad.doubleclick.net/imp;v7;x;222296909;0-0;0;45922216;0/0;35460632/35478450/2;;~aopt=0/0/ff/0;~cs=h%3fhttp://s0.2mdn.net/dot.gif]]></impression> <creatives> <creative sequence="1" adid=""> <linear> <duration>00:00:52</duration> <trackingevents> <tracking event="start"><![cdata[http://ad.doubleclick.net/activity;src=1379578;met=1;v=1;pid=45922216;aid=222296909;ko=0;cid=35460632;rid=35478450;rv=2;timestamp=2886483;eid1=11;ecn1=1;etm1=0;]]></tracking> <tracking event="midpoint"><![cdata[http://ad.doubleclick.net/activity;src=1379578;met=1;v=1;pid=45922216;aid=222296909;ko=0;cid=35460632;rid=35478450;rv=2;timestamp=2886483;eid1=18;ecn1=1;etm1=0;]]></tracking> <tracking event="complete"><![cdata[http://ad.doubleclick.net/activity;src=1379578;met=1;v=1;pid=45922216;aid=222296909;ko=0;cid=35460632;rid=35478450;rv=2;timestamp=2886483;eid1=13;ecn1=1;etm1=0;]]></tracking> <tracking event="mute"><![cdata[http://ad.doubleclick.net/activity;src=1379578;met=1;v=1;pid=45922216;aid=222296909;ko=0;cid=35460632;rid=35478450;rv=2;timestamp=2886483;eid1=16;ecn1=1;etm1=0;]]></tracking> <tracking event="pause"><![cdata[http://ad.doubleclick.net/activity;src=1379578;met=1;v=1;pid=45922216;aid=222296909;ko=0;cid=35460632;rid=35478450;rv=2;timestamp=2886483;eid1=15;ecn1=1;etm1=0;]]></tracking> <tracking event="fullscreen"><![cdata[http://ad.doubleclick.net/activity;src=1379578;met=1;v=1;pid=45922216;aid=222296909;ko=0;cid=35460632;rid=35478450;rv=2;timestamp=2886483;eid1=19;ecn1=1;etm1=0;]]></tracking> </trackingevents> <adparameters /> <videoclicks> <clickthrough><![cdata[http://google.com]]></clickthrough> <clicktracking id="dart"><![cdata[http://ad.doubleclick.net/click%3bh%3dv8/3fae/3/0/%2a/f%3b222296909%3b0-0%3b0%3b45922216%3b255-0/0%3b35460632/35478450/2%3b%3b%7eaopt%3d0/0/ff/0%3b%7esscs%3d%3fhttp://s0.2mdn.net/dot.gif]]></clicktracking> </videoclicks> <mediafiles> <mediafile id="1" delivery="streaming" type="video/x-flv" bitrate="766" width="854" height="480"><![cdata[rtmp://rmcdn.f.2mdn.net/ondemand/motiffiles/html/1379578/parisian_love_126566284014011.flv]]></mediafile> <mediafile id="2" delivery="progressive" type="video/x-flv" bitrate="766" width="854" height="480"><![cdata[http://gcdn.2mdn.net/motiffiles/html/1379578/parisian_love_126566284014011.flv]]></mediafile> </mediafiles> </linear> </creative> <creative sequence="1" adid=""> <companionads> <companion id="1" width="300" height="250"> <staticresource creativetype="application/x-shockwave-flash"><![cdata[http://s0.2mdn.net/1379578/pid_1244559_1265644799000_parisian_love_300x250.swf]]></staticresource> <companionclickthrough><![cdata[http://google.com]]></companionclickthrough> <alttext /> <adparameters /> </companion> <companion id="2" width="300" height="250"> <staticresource creativetype="image/jpeg"><![cdata[http://s0.2mdn.net/1379578/pid_1244559_1265644904000_parisian_love_300x250.jpg]]></staticresource> <companionclickthrough><![cdata[http://google.com]]></companionclickthrough> <alttext /> <adparameters /> </companion> <companion id="3" width="728" height="90"> <staticresource creativetype="image/jpeg"><![cdata[http://s0.2mdn.net/1379578/pid_1244559_1265644932000_parisian_love_728x90.jpg]]></staticresource> <companionclickthrough><![cdata[http://google.com]]></companionclickthrough> <alttext /> <adparameters /> </companion> <companion id="4" width="728" height="90"> <staticresource creativetype="application/x-shockwave-flash"><![cdata[http://s0.2mdn.net/1379578/pid_1244559_1265644945000_parisian_love_728x90.swf]]></staticresource> <companionclickthrough><![cdata[http://google.com]]></companionclickthrough> <alttext /> <adparameters /> </companion> </companionads> </creative> <creative sequence="2" adid=""> <nonlinearads> <trackingevents> <tracking event="start"><![cdata[http://ad.doubleclick.net/activity;src=1379578;met=1;v=1;pid=45922216;aid=222296909;ko=0;cid=35460632;rid=35478450;rv=2;timestamp=2886483;eid1=11;ecn1=1;etm1=0;]]></tracking> <tracking event="midpoint"><![cdata[http://ad.doubleclick.net/activity;src=1379578;met=1;v=1;pid=45922216;aid=222296909;ko=0;cid=35460632;rid=35478450;rv=2;timestamp=2886483;eid1=18;ecn1=1;etm1=0;]]></tracking> <tracking event="complete"><![cdata[http://ad.doubleclick.net/activity;src=1379578;met=1;v=1;pid=45922216;aid=222296909;ko=0;cid=35460632;rid=35478450;rv=2;timestamp=2886483;eid1=13;ecn1=1;etm1=0;]]></tracking> <tracking event="mute"><![cdata[http://ad.doubleclick.net/activity;src=1379578;met=1;v=1;pid=45922216;aid=222296909;ko=0;cid=35460632;rid=35478450;rv=2;timestamp=2886483;eid1=16;ecn1=1;etm1=0;]]></tracking> <tracking event="pause"><![cdata[http://ad.doubleclick.net/activity;src=1379578;met=1;v=1;pid=45922216;aid=222296909;ko=0;cid=35460632;rid=35478450;rv=2;timestamp=2886483;eid1=15;ecn1=1;etm1=0;]]></tracking> <tracking event="fullscreen"><![cdata[http://ad.doubleclick.net/activity;src=1379578;met=1;v=1;pid=45922216;aid=222296909;ko=0;cid=35460632;rid=35478450;rv=2;timestamp=2886483;eid1=19;ecn1=1;etm1=0;]]></tracking> </trackingevents> <nonlinear id="1" width="400" height="150" scalable="false" maintainaspectratio="false"> <staticresource creativetype="image/jpeg"><![cdata[http://s0.2mdn.net/1379578/pid_1244559_1281625520000_google.jpg]]></staticresource> <nonlinearclickthrough><![cdata[http://google.com]]></nonlinearclickthrough> <adparameters /> </nonlinear> </nonlinearads> </creative> </creatives> <extensions> <extension type="dart"> <adservingdata> <deliverydata> <geodata><![cdata[ct=us&st=ga&ac=404&zp=30319&bw=0&dma=26&city=13241]]></geodata> </deliverydata> </adservingdata> </extension> </extensions> </inline> </ad> </vast>
easy, append element node new document.
$source = new domdocument(); $source->loadxml($samplexml); $xpath = new domxpath($dom); $target = new domdocument(); $target->appendchild( $target->importnode($xpath->evaluate('//vast')->item(0), true) ); echo $target->savexml();
Comments
Post a Comment