?
Solved

Ideas to parse dynamic xml file

Posted on 2012-08-13
6
Medium Priority
?
205 Views
Last Modified: 2012-08-28
Dear All:

Can anyone provide some ideas how to deal with dynamic XML file with php?

For example:
<xml>
<REVST REVCAP="n">
<SUBCH REVDATE="2012.09.06" SECTNBR="20" SUBJNBR="00" CHAPNBR="05" ASSNBR="62" CHG="R" KEY="subch05200062">
  <TITLE>ROTOR PRINCIPAL</TITLE> 
<SUBUD REVDATE="2012.09.06" SECTNBR="20" SUBJNBR="00" CHAPNBR="05" ASSNBR="6210" CHG="N" KEY="sub0520006210">
<EFFECT>
  <TITLE>PALE PRINCIPALE</TITLE> 
<CYCLE REVDATE="" CHG="u" SECTNBR="20" SUBJNBR="00" CHAPNBR="05" CYCLNBR="0" CYCLUNIT="ALF">
<MAINTASK IDENTASK="62/10/20/601/000/090" REVDATE="" TYPETASK="" VERSION="B2,B3" CHG="N" MSMFREQUENCY="Periodic" AMTOSS="-">
  <EFFECDOC /> 
  <TITLE>Paleprincipale(ALF)</TITLE> 
<MAINCHEC>
<LIMIT TYPE="-" MSMGEOGRAPHIC="">
<CLASS CATEGORY="0">
<NEXTPERF>
<INTERV MSMLIMITORDER="1" MSMTEMPORARYLIMIT="False">
  <VAL>0</VAL> 
  <UNIT>ALF</UNIT> 
<MARGIN>
  <VAL>0</VAL> 
  <UNIT>-</UNIT> 
  </MARGIN>
<MARGINMIN>
  <VAL>0</VAL> 
  <UNIT>-</UNIT> 
  </MARGINMIN>
<MARGINMAX>
  <VAL>0</VAL> 
  <UNIT>-</UNIT> 
  </MARGINMAX>
  </INTERV>
  </NEXTPERF>
<REFDOC REFLOC="05-40-00,6-6" REFMAN="AMM">
  <SREFLOC>05-40-00,6-6</SREFLOC> 
  <SREFMAN>AMM</SREFMAN> 
  </REFDOC>
  </CLASS>
  </LIMIT>
  </MAINCHEC>
<DESC>
  <PARA>Vrification visuelle.</PARA> 
  </DESC>
<AMTOSS CODE="-">
  <PARA /> 
  </AMTOSS>
<CLIMATICCONDITIONLIST>
<CLIMATICCONDITION CODE="-">
  <PARA /> 
  </CLIMATICCONDITION>
  </CLIMATICCONDITIONLIST>
<TEXTUALAPPLICABILITY>
  <PARA>VC.</PARA> 
  </TEXTUALAPPLICABILITY>
<AMSAPPLICABILITY>
  <PARA>PRE MOD 075057 // PRE MOD 075058</PARA> 
  </AMSAPPLICABILITY>
  </MAINTASK>
  </CYCLE>
  </EFFECT>
  </SUBUD>
  </SUBCH>
<REVEND>
<REVST REVCAP="n">
<SUBCH REVDATE="2012.09.06" SECTNBR="20" SUBJNBR="00" CHAPNBR="05" ASSNBR="64" CHG="N" KEY="subch05200064">
  <TITLE>ROTOR ARRIERE</TITLE> 
<SUBUD REVDATE="2012.09.06" SECTNBR="20" SUBJNBR="00" CHAPNBR="05" ASSNBR="6410" CHG="N" KEY="sub0520006410">
<EFFECT>
  <TITLE>ROTOR ANTI COUPLE</TITLE> 
<CYCLE REVDATE="" CHG="u" SECTNBR="20" SUBJNBR="00" CHAPNBR="05" CYCLNBR="0" CYCLUNIT="ALF">
<MAINTASK IDENTASK="64/10/00/000/000/070" REVDATE="" TYPETASK="" VERSION="B2,B3" CHG="N" MSMFREQUENCY="Periodic" AMTOSS="-">
  <EFFECDOC /> 
  <TITLE>Palerotorarrire(ALF)</TITLE> 
<MAINCHEC>
<LIMIT TYPE="-" MSMGEOGRAPHIC="">
<CLASS CATEGORY="0">
<NEXTPERF>
<INTERV MSMLIMITORDER="1" MSMTEMPORARYLIMIT="False">
  <VAL>0</VAL> 
  <UNIT>ALF</UNIT> 
<MARGIN>
  <VAL>0</VAL> 
  <UNIT>-</UNIT> 
  </MARGIN>
<MARGINMIN>
  <VAL>0</VAL> 
  <UNIT>-</UNIT> 
  </MARGINMIN>
<MARGINMAX>
  <VAL>0</VAL> 
  <UNIT>-</UNIT> 
  </MARGINMAX>
  </INTERV>
  </NEXTPERF>
<REFDOC REFLOC="05-40-00,6-6" REFMAN="AMM">
  <SREFLOC>05-40-00,6-6</SREFLOC> 
  <SREFMAN>AMM</SREFMAN> 
  </REFDOC>
  </CLASS>
  </LIMIT>
  </MAINCHEC>
<DESC>
  <PARA>Vrification visuelle.</PARA> 
  </DESC>
<AMTOSS CODE="-">
  <PARA /> 
  </AMTOSS>
<CLIMATICCONDITIONLIST>
<CLIMATICCONDITION CODE="-">
  <PARA /> 
  </CLIMATICCONDITION>
  </CLIMATICCONDITIONLIST>
<TEXTUALAPPLICABILITY>
  <PARA>VC.</PARA> 
  </TEXTUALAPPLICABILITY>
<AMSAPPLICABILITY>
  <PARA>PRE MOD 075580</PARA> 
  </AMSAPPLICABILITY>
  </MAINTASK>
  </CYCLE>
  </EFFECT>
  </SUBUD>
  </SUBCH>
<REVEND>
.........
</xml>

Open in new window


sometime, the xml may start
->REVST->SUBCH
->REVST->REVEND->SUBCH
->REVST->REVEND->REVST->SUBCH
->REVST->REVEND->REVST->REVEND->REVST->SUBCH
and etc.....


Thank you.
0
Comment
Question by:eurocoptersea
6 Comments
 
LVL 25

Expert Comment

by:lwadwell
ID: 38286784
You could try XPath addressing ... e.g.

foreach ( $obj->xpath('//SUBCH') as $ele ) {
	var_dump($e);
}

Open in new window

0
 
LVL 61

Expert Comment

by:Julian Hansen
ID: 38286827
Have you had a look at the SimpleXML functions?

http://in2.php.net/manual/en/book.simplexml.php
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 38286919
I would use simplexml as in the example below.  I had to modify the XML you posted as it contained mis-matched tags. A sample of the output looks like this

revcap n
revDate 2012.09.06
revSect 20


--next group --
revcap n
revDate 2012.09.06
revSect 20

and the code showing how to do this is below (I did not do all the data fields, just enough to illustrate the point)


<?php

$testData = '<xml>
<REVST REVCAP="n">
<SUBCH REVDATE="2012.09.06" SECTNBR="20" SUBJNBR="00" CHAPNBR="05" ASSNBR="62" CHG="R" KEY="subch05200062">
  <TITLE>ROTOR PRINCIPAL</TITLE>
<SUBUD REVDATE="2012.09.06" SECTNBR="20" SUBJNBR="00" CHAPNBR="05" ASSNBR="6210" CHG="N" KEY="sub0520006210">
<EFFECT>
  <TITLE>PALE PRINCIPALE</TITLE>
<CYCLE REVDATE="" CHG="u" SECTNBR="20" SUBJNBR="00" CHAPNBR="05" CYCLNBR="0" CYCLUNIT="ALF">
<MAINTASK IDENTASK="62/10/20/601/000/090" REVDATE="" TYPETASK="" VERSION="B2,B3" CHG="N" MSMFREQUENCY="Periodic" AMTOSS="-">
  <EFFECDOC />
  <TITLE>Paleprincipale(ALF)</TITLE>
<MAINCHEC>
<LIMIT TYPE="-" MSMGEOGRAPHIC="">
<CLASS CATEGORY="0">
<NEXTPERF>
<INTERV MSMLIMITORDER="1" MSMTEMPORARYLIMIT="False">
  <VAL>0</VAL>
  <UNIT>ALF</UNIT>
<MARGIN>
  <VAL>0</VAL>
  <UNIT>-</UNIT>
  </MARGIN>
<MARGINMIN>
  <VAL>0</VAL>
  <UNIT>-</UNIT>
  </MARGINMIN>
<MARGINMAX>
  <VAL>0</VAL>
  <UNIT>-</UNIT>
  </MARGINMAX>
  </INTERV>
  </NEXTPERF>
<REFDOC REFLOC="05-40-00,6-6" REFMAN="AMM">
  <SREFLOC>05-40-00,6-6</SREFLOC>
  <SREFMAN>AMM</SREFMAN>
  </REFDOC>
  </CLASS>
  </LIMIT>
  </MAINCHEC>
<DESC>
  <PARA>Vrification visuelle.</PARA>
  </DESC>
<AMTOSS CODE="-">
  <PARA />
  </AMTOSS>
<CLIMATICCONDITIONLIST>
<CLIMATICCONDITION CODE="-">
  <PARA />
  </CLIMATICCONDITION>
  </CLIMATICCONDITIONLIST>
<TEXTUALAPPLICABILITY>
  <PARA>VC.</PARA>
  </TEXTUALAPPLICABILITY>
<AMSAPPLICABILITY>
  <PARA>PRE MOD 075057 // PRE MOD 075058</PARA>
  </AMSAPPLICABILITY>
  </MAINTASK>
  </CYCLE>
  </EFFECT>
  </SUBUD>
  </SUBCH>
</REVST>
<REVST REVCAP="n">
<SUBCH REVDATE="2012.09.06" SECTNBR="20" SUBJNBR="00" CHAPNBR="05" ASSNBR="64" CHG="N" KEY="subch05200064">
  <TITLE>ROTOR ARRIERE</TITLE>
<SUBUD REVDATE="2012.09.06" SECTNBR="20" SUBJNBR="00" CHAPNBR="05" ASSNBR="6410" CHG="N" KEY="sub0520006410">
<EFFECT>
  <TITLE>ROTOR ANTI COUPLE</TITLE>
<CYCLE REVDATE="" CHG="u" SECTNBR="20" SUBJNBR="00" CHAPNBR="05" CYCLNBR="0" CYCLUNIT="ALF">
<MAINTASK IDENTASK="64/10/00/000/000/070" REVDATE="" TYPETASK="" VERSION="B2,B3" CHG="N" MSMFREQUENCY="Periodic" AMTOSS="-">
  <EFFECDOC />
  <TITLE>Palerotorarrire(ALF)</TITLE>
<MAINCHEC>
<LIMIT TYPE="-" MSMGEOGRAPHIC="">
<CLASS CATEGORY="0">
<NEXTPERF>
<INTERV MSMLIMITORDER="1" MSMTEMPORARYLIMIT="False">
  <VAL>0</VAL>
  <UNIT>ALF</UNIT>
<MARGIN>
  <VAL>0</VAL>
  <UNIT>-</UNIT>
  </MARGIN>
<MARGINMIN>
  <VAL>0</VAL>
  <UNIT>-</UNIT>
  </MARGINMIN>
<MARGINMAX>
  <VAL>0</VAL>
  <UNIT>-</UNIT>
  </MARGINMAX>
  </INTERV>
  </NEXTPERF>
<REFDOC REFLOC="05-40-00,6-6" REFMAN="AMM">
  <SREFLOC>05-40-00,6-6</SREFLOC>
  <SREFMAN>AMM</SREFMAN>
  </REFDOC>
  </CLASS>
  </LIMIT>
  </MAINCHEC>
<DESC>
  <PARA>Vrification visuelle.</PARA>
  </DESC>
<AMTOSS CODE="-">
  <PARA />
  </AMTOSS>
<CLIMATICCONDITIONLIST>
<CLIMATICCONDITION CODE="-">
  <PARA />
  </CLIMATICCONDITION>
  </CLIMATICCONDITIONLIST>
<TEXTUALAPPLICABILITY>
  <PARA>VC.</PARA>
  </TEXTUALAPPLICABILITY>
<AMSAPPLICABILITY>
  <PARA>PRE MOD 075580</PARA>
  </AMSAPPLICABILITY>
  </MAINTASK>
  </CYCLE>
  </EFFECT>
  </SUBUD>
  </SUBCH>
</REVST>
</xml>
';


// Load

$xml = simplexml_load_string( $testData );

// Example display
//
echo "<pre>";
print_r($xml);
echo "</pre>";

// Example process
//
echo "<hr/>";

foreach( $xml->REVST as $element ) {

     // Attributes
     //
     $att = $element->attributes();
     
     $revcap = (string) $att['REVCAP'];
     $subChAtt = $element->SUBCH->attributes();
     $subchRevDate = (string) $subChAtt ['REVDATE'];
     $subchSectNbr = (int) $subChAtt ['SECTNBR'];
     // etc....
     
     $subchTitle = (string) $element->SUBCH->TITLE;
     echo "revcap $revcap<br/>";
     echo "revDate $subchRevDate<br/>";
     echo "revSect $subchSectNbr<br/>";
     echo "<br/>";
     echo "<br/>";
     echo "--next group -- <br/>";

}



                                  

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 25

Accepted Solution

by:
lwadwell earned 1500 total points
ID: 38287064
Sorry ... I assumed you were already using SimpleXML within PHP ... the XPath suggestion is part of SimpleXML.  Example script below:
<?php
echo "<pre>";

$filename = '/Apps/DATA/Q_27826441.xml';
if (file_exists($filename)) {
    $xml = simplexml_load_file($filename);
} else {
    exit("Failed to open $filename.");
}

foreach ( $xml->xpath('//SUBCH') as $ele ) {
	echo "Title:   ", $ele->TITLE, PHP_EOL;
	echo "Revdate: ", $ele->attributes()->REVDATE, PHP_EOL;
	echo "Effect Title:   ", $ele->SUBUD->EFFECT->TITLE, PHP_EOL;
	//print_r($ele);
}
?>

Open in new window

Q-27826441.xml
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38287156
See http://www.laprbass.com/RAY_temp_eurocoptersea.php

Please see lines 128-130 of the code snippet.  These had to be changed to make the XML parse correctly.  The good news is that it can now be parsed with SimpleXML and that means you can use object-oriented notation to access the properties of the object.

The bad news is that this is a terrible XML structure.  It makes no sense at all from a computer science point of view to have the same tag name take on different semantic meanings when it is used in a different position in the XML string.  And from a human-factors point of view, it would make all the sense in the world to use consistent indenting to visualize the hierarchy of the data.  If you have any influence with the publisher of the XML you should ask for someone with experience in XML to help the publisher revise this structure and layout.  

<?php // RAY_temp_euocoptersea.php
error_reporting(E_ALL);
echo "<pre>";

$xml = <<<XML
<xml>
<REVST REVCAP="n">
<SUBCH REVDATE="2012.09.06" SECTNBR="20" SUBJNBR="00" CHAPNBR="05" ASSNBR="62" CHG="R" KEY="subch05200062">
  <TITLE>ROTOR PRINCIPAL</TITLE>
<SUBUD REVDATE="2012.09.06" SECTNBR="20" SUBJNBR="00" CHAPNBR="05" ASSNBR="6210" CHG="N" KEY="sub0520006210">
<EFFECT>
  <TITLE>PALE PRINCIPALE</TITLE>
<CYCLE REVDATE="" CHG="u" SECTNBR="20" SUBJNBR="00" CHAPNBR="05" CYCLNBR="0" CYCLUNIT="ALF">
<MAINTASK IDENTASK="62/10/20/601/000/090" REVDATE="" TYPETASK="" VERSION="B2,B3" CHG="N" MSMFREQUENCY="Periodic" AMTOSS="-">
  <EFFECDOC />
  <TITLE>Paleprincipale(ALF)</TITLE>
<MAINCHEC>
<LIMIT TYPE="-" MSMGEOGRAPHIC="">
<CLASS CATEGORY="0">
<NEXTPERF>
<INTERV MSMLIMITORDER="1" MSMTEMPORARYLIMIT="False">
  <VAL>0</VAL>
  <UNIT>ALF</UNIT>
<MARGIN>
  <VAL>0</VAL>
  <UNIT>-</UNIT>
  </MARGIN>
<MARGINMIN>
  <VAL>0</VAL>
  <UNIT>-</UNIT>
  </MARGINMIN>
<MARGINMAX>
  <VAL>0</VAL>
  <UNIT>-</UNIT>
  </MARGINMAX>
  </INTERV>
  </NEXTPERF>
<REFDOC REFLOC="05-40-00,6-6" REFMAN="AMM">
  <SREFLOC>05-40-00,6-6</SREFLOC>
  <SREFMAN>AMM</SREFMAN>
  </REFDOC>
  </CLASS>
  </LIMIT>
  </MAINCHEC>
<DESC>
  <PARA>Vrification visuelle.</PARA>
  </DESC>
<AMTOSS CODE="-">
  <PARA />
  </AMTOSS>
<CLIMATICCONDITIONLIST>
<CLIMATICCONDITION CODE="-">
  <PARA />
  </CLIMATICCONDITION>
  </CLIMATICCONDITIONLIST>
<TEXTUALAPPLICABILITY>
  <PARA>VC.</PARA>
  </TEXTUALAPPLICABILITY>
<AMSAPPLICABILITY>
  <PARA>PRE MOD 075057 // PRE MOD 075058</PARA>
  </AMSAPPLICABILITY>
  </MAINTASK>
  </CYCLE>
  </EFFECT>
  </SUBUD>
  </SUBCH>
<REVEND>
<REVST REVCAP="n">
<SUBCH REVDATE="2012.09.06" SECTNBR="20" SUBJNBR="00" CHAPNBR="05" ASSNBR="64" CHG="N" KEY="subch05200064">
  <TITLE>ROTOR ARRIERE</TITLE>
<SUBUD REVDATE="2012.09.06" SECTNBR="20" SUBJNBR="00" CHAPNBR="05" ASSNBR="6410" CHG="N" KEY="sub0520006410">
<EFFECT>
  <TITLE>ROTOR ANTI COUPLE</TITLE>
<CYCLE REVDATE="" CHG="u" SECTNBR="20" SUBJNBR="00" CHAPNBR="05" CYCLNBR="0" CYCLUNIT="ALF">
<MAINTASK IDENTASK="64/10/00/000/000/070" REVDATE="" TYPETASK="" VERSION="B2,B3" CHG="N" MSMFREQUENCY="Periodic" AMTOSS="-">
  <EFFECDOC />
  <TITLE>Palerotorarrire(ALF)</TITLE>
<MAINCHEC>
<LIMIT TYPE="-" MSMGEOGRAPHIC="">
<CLASS CATEGORY="0">
<NEXTPERF>
<INTERV MSMLIMITORDER="1" MSMTEMPORARYLIMIT="False">
  <VAL>0</VAL>
  <UNIT>ALF</UNIT>
<MARGIN>
  <VAL>0</VAL>
  <UNIT>-</UNIT>
  </MARGIN>
<MARGINMIN>
  <VAL>0</VAL>
  <UNIT>-</UNIT>
  </MARGINMIN>
<MARGINMAX>
  <VAL>0</VAL>
  <UNIT>-</UNIT>
  </MARGINMAX>
  </INTERV>
  </NEXTPERF>
<REFDOC REFLOC="05-40-00,6-6" REFMAN="AMM">
  <SREFLOC>05-40-00,6-6</SREFLOC>
  <SREFMAN>AMM</SREFMAN>
  </REFDOC>
  </CLASS>
  </LIMIT>
  </MAINCHEC>
<DESC>
  <PARA>Vrification visuelle.</PARA>
  </DESC>
<AMTOSS CODE="-">
  <PARA />
  </AMTOSS>
<CLIMATICCONDITIONLIST>
<CLIMATICCONDITION CODE="-">
  <PARA />
  </CLIMATICCONDITION>
  </CLIMATICCONDITIONLIST>
<TEXTUALAPPLICABILITY>
  <PARA>VC.</PARA>
  </TEXTUALAPPLICABILITY>
<AMSAPPLICABILITY>
  <PARA>PRE MOD 075580</PARA>
  </AMSAPPLICABILITY>
  </MAINTASK>
  </CYCLE>
  </EFFECT>
  </SUBUD>
  </SUBCH>
</REVST>
</REVEND>
</REVST>
</xml>
XML;

// MAKE AN OBJECT
$obj = SimpleXML_Load_String($xml);

// ACTIVATE THIS TO SEE THE OBJECT
var_dump($obj);

Open in new window

Best of luck with your project, ~Ray
0
 

Author Closing Comment

by:eurocoptersea
ID: 38339818
Thanks .
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question