2016-03-15 164 views
1

我試圖獲取它,以便對於概念類型列中的每個概念類型,它顯示不同的xml代碼,具體取決於概念類型是什麼。循環遍歷一個XML文件並在表格中顯示

到目前爲止我有這個代碼。

<?php 
error_reporting(1); 
$dom = new DOMDocument('1.0', 'utf-8'); 
$dom->formatOutput = true; 
$dom->preserveWhiteSpace = false; 
#include_once upload.php'; 
$name = $_FILES['file']['name']; 
$dom->load($name); 
$cogxml = simplexml_load_file($name); 

$extension = strtolower(substr($name, strpos($name, '.') + 1)); 
#$size = $_FILES['file']['size']; 
# $type = $_FILES['file']['type']; 
$tmp_name = $_FILES['file']['tmp_name']; 

if(isset ($name)){ 
    if(!empty($name)) { 
     if(($extension=='cogxml' || $extension=='cgx')){ 
      $location = ''; 

     if (move_uploaded_file($tmp_name, $location.$name)){ 
      echo 'uploaded'; 
     }else{ 
      echo 'There was an error'; 
     } 
    }else{ 
     echo 'File must be cogxml or cgx.'; 
    } 
} 
} 


?> 

<?php 

      ?> 

    <html> 
    <head> 
    <title>Parsing CharGer and CoGui</title> 
    <link rel="stylesheet" type="text/css" href="css.css" /> 
    </head> 
    <body> 
<table border=1> 
<tr><th>Concept Name</th><th>Relation Type</th><th>CoGui XML</th><th>CharGer XML</th></tr> 
<?php foreach ($cogxml->support->conceptTypes->ctype as $data) { 
         #foreach ($name->support->relationTypes->rtype as $relationType){ 

         ?> 
         <tr><td><?php echo $data['label']; ?></td><td></td> 

<?php $newdom = new DOMDocument('1.0', 'utf-8'); 
    $newdom->formatOutput = true; 
$newdom->preserveWhiteSpace = false; 

$xpath = new DOMXPath($dom); 

foreach ($dom->getElementsByTagName("ctype") as $ctype) { 

    $newdom->loadXML("<cogxml><support><conceptTypes /><relationTypes /></support></cogxml>"); 

    $newnode = $newdom->importNode($ctype, true); 
    $newdom->getElementsByTagName("conceptTypes")->item(0)->appendChild($newnode); 

    $id = $ctype->getAttribute('id'); 

    foreach ($xpath->query("//cogxml/support/conceptTypes/order[@id1='$id']") as $order) { 
     $newnode = $newdom->importNode($order, true); 
     $newdom->getElementsByTagName("conceptTypes")->item(0)->appendChild($newnode); 
    }  

    foreach ($xpath->query("//cogxml/support/relationTypes/rtype[contains(@idSignature, '$id')]") as $rtype) { 
     $newnode = $newdom->importNode($rtype, true); 
     $newdom->getElementsByTagName("relationTypes")->item(0)->appendChild($newnode); 
} } 
?><td><xmp><?php echo $newdom->saveXML();?></xmp></td><td></td></tr><?php }?></table> 

    <form action="test4.php" method="POST" enctype="multipart/form-data"> 
    <input type="file" name="file"><br><br> 
    <input type="submit" value="Submit"> 
</form> 
</body> 
</html> 

$ name是您在表單中上傳的文件。下面的代碼是我的xml代碼看起來像的一個例子。

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<cogxml> 
<namespace name="http://www.lirmm.fr/cogui#" prefix=""/> 
<support name="vocabulary"> 
<conceptTypes> 
<ctype id="http://www.lirmm.fr/cogui#ct_043ea910-5f86-4150-b0f1-1418acf4db39" label="Junior Employee" x="250" y="10"> 
<translation descr="" label="Junior Employee" lang="en"/> 
</ctype> 
<ctype id="http://www.lirmm.fr/cogui#ct_d7a78641-722f-4609-8f5a-90affc111e00" label="Employee" x="130" y="60"> 
<translation descr="" label="Employee" lang="en"/> 
</ctype> 
<ctype id="http://www.lirmm.fr/cogui#ct_feeca670-2f1c-433e-9271-4cffeda1e929" label="Director" x="250" y="110"> 
<translation descr="" label="Director" lang="en"/> 
</ctype> 
<ctype id="http://www.lirmm.fr/cogui#ct_710bed80-a33e-4a13-b916-15fbb3357e8d" label="Manager" x="250" y="60"> 
<translation descr="" label="Manager" lang="en"/> 
</ctype> 
<ctype id="http://www.lirmm.fr/cogui#ct_cd84c648-ef22-4854-8e8c-a6654c0386be" label="Senior Employee" x="255" y="190"> 
<translation descr="" label="Senior Employee" lang="en"/> 
</ctype> 
<ctype id="http://www.lirmm.fr/cogui#_ct_a12bacc5-bc88-429e-a7b1-45e143591288" label="Top" x="10" y="60"> 
<translation descr="" label="Top" lang="en"/> 
</ctype> 
<order id1="http://www.lirmm.fr/cogui#ct_cd84c648-ef22-4854-8e8c-a6654c0386be" id2="http://www.lirmm.fr/cogui#ct_d7a78641-722f-4609-8f5a-90affc111e00"/> 
<order id1="http://www.lirmm.fr/cogui#ct_feeca670-2f1c-433e-9271-4cffeda1e929" id2="http://www.lirmm.fr/cogui#ct_d7a78641-722f-4609-8f5a-90affc111e00"/> 
<order id1="http://www.lirmm.fr/cogui#ct_d7a78641-722f-4609-8f5a-90affc111e00" id2="http://www.lirmm.fr/cogui#_ct_a12bacc5-bc88-429e-a7b1-45e143591288"/> 
<order id1="http://www.lirmm.fr/cogui#ct_043ea910-5f86-4150-b0f1-1418acf4db39" id2="http://www.lirmm.fr/cogui#ct_d7a78641-722f-4609-8f5a-90affc111e00"/> 
<order id1="http://www.lirmm.fr/cogui#ct_710bed80-a33e-4a13-b916-15fbb3357e8d" id2="http://www.lirmm.fr/cogui#ct_d7a78641-722f-4609-8f5a-90affc111e00"/> 
</conceptTypes> 
<relationTypes> 
<rtype id="http://www.lirmm.fr/cogui#_rt_c42a5ce6-2f20-491d-8c91-501ae178a36c" idSignature="http://www.lirmm.fr/cogui#_ct_a12bacc5-bc88-429e-a7b1-45e143591288 http://www.lirmm.fr/cogui#_ct_a12bacc5-bc88-429e-a7b1-45e143591288" label="Link" x="10.0" y="10.0"> 
<translation descr="" label="Link" lang="en"/> 
</rtype> 
<rtype id="http://www.lirmm.fr/cogui#rt_af40394c-9e62-4e92-b05b-352de5db876f" idSignature="http://www.lirmm.fr/cogui#_ct_a12bacc5-bc88-429e-a7b1-45e143591288 http://www.lirmm.fr/cogui#_ct_a12bacc5-bc88-429e-a7b1-45e143591288" label="senior" x="70.0" y="10.0"> 
<translation descr="" label="senior" lang="en"/> 
</rtype> 
<order id1="http://www.lirmm.fr/cogui#rt_af40394c-9e62-4e92-b05b-352de5db876f" id2="http://www.lirmm.fr/cogui#_rt_c42a5ce6-2f20-491d-8c91-501ae178a36c"/> 
</relationTypes> 
<nestingTypes> 
<ntype id="http://www.lirmm.fr/cogui#_nt_4d626655-02b5-496e-b19c-f4cdb72ee70a" label="Nesting"> 
<translation descr="" label="Nesting" lang="en"/> 
</ntype> 
</nestingTypes> 
<conformity> 
<marker id="http://www.lirmm.fr/cogui#i_435d513c-1b39-43b1-9b6d-310fa0ee46d3" idType="http://www.lirmm.fr/cogui#ct_710bed80-a33e-4a13-b916-15fbb3357e8d" label="Lucy"/> 
<marker id="http://www.lirmm.fr/cogui#i_80311575-7d72-4af7-bdbe-a19c4bbcf248" idType="http://www.lirmm.fr/cogui#ct_cd84c648-ef22-4854-8e8c-a6654c0386be" label="Simon"/> 
<marker id="http://www.lirmm.fr/cogui#i_d1a8babc-3d35-4580-b4d5-d3cd4c323c98" idType="http://www.lirmm.fr/cogui#ct_043ea910-5f86-4150-b0f1-1418acf4db39" label="Robyn"/> 
<marker id="http://www.lirmm.fr/cogui#i_efbef15b-d6e2-4c0a-8155-ecaae75cc673" idType="http://www.lirmm.fr/cogui#ct_feeca670-2f1c-433e-9271-4cffeda1e929" label="Richard"/> 
</conformity> 
<modules/> 
</support> 
<localeTypes name="undefined_vocabulary"> 
<conceptTypes/> 
<relationTypes/> 
<nestingTypes/> 
<conformity/> 
<modules/> 
</localeTypes> 
<graph id="_g1" label="seniorities" nature="fact" set="default_set"> 
<concept id="c_f55e1936-7842-4518-b460-bb34a9000871" idMarker="http://www.lirmm.fr/cogui#i_d1a8babc-3d35-4580-b4d5-d3cd4c323c98" idType="http://www.lirmm.fr/cogui#ct_043ea910-5f86-4150-b0f1-1418acf4db39" referent="individual" x="0" y="0"/> 
<concept id="c_f081c276-57ff-4650-94ec-6e40dfd38023" idMarker="http://www.lirmm.fr/cogui#i_80311575-7d72-4af7-bdbe-a19c4bbcf248" idType="http://www.lirmm.fr/cogui#ct_cd84c648-ef22-4854-8e8c-a6654c0386be" referent="individual" x="185" y="70"/> 
<concept id="c_f0229ed1-c913-4373-af9c-361a90a56e90" idMarker="http://www.lirmm.fr/cogui#i_435d513c-1b39-43b1-9b6d-310fa0ee46d3" idType="http://www.lirmm.fr/cogui#ct_710bed80-a33e-4a13-b916-15fbb3357e8d" referent="individual" x="330" y="170"/> 
<concept id="c_dbe5b7cb-7d00-44f1-8b9a-832d5b61a126" idMarker="http://www.lirmm.fr/cogui#i_efbef15b-d6e2-4c0a-8155-ecaae75cc673" idType="http://www.lirmm.fr/cogui#ct_feeca670-2f1c-433e-9271-4cffeda1e929" referent="individual" x="445" y="260"/> 
<relation id="r_10008dd3-5426-4c87-8651-049045f98376" idType="http://www.lirmm.fr/cogui#rt_af40394c-9e62-4e92-b05b-352de5db876f" x="90" y="70"/> 
<relation id="r_8ef0b4bf-3cb6-4dde-9c83-903cb459872a" idType="http://www.lirmm.fr/cogui#rt_af40394c-9e62-4e92-b05b-352de5db876f" x="235" y="135"/> 
<relation id="r_563d1804-04eb-45d4-81c9-f4bd4782e0b1" idType="http://www.lirmm.fr/cogui#rt_af40394c-9e62-4e92-b05b-352de5db876f" x="355" y="260"/> 
<edge cid="c_f55e1936-7842-4518-b460-bb34a9000871" label="1" rid="r_10008dd3-5426-4c87-8651-049045f98376"/> 
<edge cid="c_f081c276-57ff-4650-94ec-6e40dfd38023" label="2" rid="r_10008dd3-5426-4c87-8651-049045f98376"/> 
<edge cid="c_f081c276-57ff-4650-94ec-6e40dfd38023" label="1" rid="r_8ef0b4bf-3cb6-4dde-9c83-903cb459872a"/> 
<edge cid="c_f0229ed1-c913-4373-af9c-361a90a56e90" label="2" rid="r_8ef0b4bf-3cb6-4dde-9c83-903cb459872a"/> 
<edge cid="c_f0229ed1-c913-4373-af9c-361a90a56e90" label="1" rid="r_563d1804-04eb-45d4-81c9-f4bd4782e0b1"/> 
<edge cid="c_dbe5b7cb-7d00-44f1-8b9a-832d5b61a126" label="2" rid="r_563d1804-04eb-45d4-81c9-f4bd4782e0b1"/> 
</graph> 
<graph id="_query1" label="Richard senior to Robyn" nature="query" set="default_set"> 
<concept id="c_90dc1159-1d02-4707-a444-2e95817d8667" idMarker="http://www.lirmm.fr/cogui#i_d1a8babc-3d35-4580-b4d5-d3cd4c323c98" idType="http://www.lirmm.fr/cogui#ct_043ea910-5f86-4150-b0f1-1418acf4db39" referent="individual" x="30" y="165"/> 
<concept id="c_4af4cf1c-5383-413b-bee2-7a4c513fd37e" idMarker="http://www.lirmm.fr/cogui#i_efbef15b-d6e2-4c0a-8155-ecaae75cc673" idType="http://www.lirmm.fr/cogui#ct_feeca670-2f1c-433e-9271-4cffeda1e929" referent="individual" x="45" y="25"/> 
<relation id="r_c51c5a9c-3cf5-4402-a356-03c9882f6b78" idType="http://www.lirmm.fr/cogui#rt_af40394c-9e62-4e92-b05b-352de5db876f" x="75" y="95"/> 
<edge cid="c_90dc1159-1d02-4707-a444-2e95817d8667" label="1" rid="r_c51c5a9c-3cf5-4402-a356-03c9882f6b78"/> 
<edge cid="c_4af4cf1c-5383-413b-bee2-7a4c513fd37e" label="2" rid="r_c51c5a9c-3cf5-4402-a356-03c9882f6b78"/> 
</graph> 
<rule id="_rule1"> 
<hypt> 
<graph id="_rule1_hypt" label="seniority rule" nature="rule" set="default_set"> 
<concept id="c_591883b6-ca82-42ee-bd35-b4ce29ffd286" idType="http://www.lirmm.fr/cogui#ct_d7a78641-722f-4609-8f5a-90affc111e00" x="30" y="45"/> 
<concept id="c_f765dff9-1cd4-42f1-bf95-87cbda00257d" idType="http://www.lirmm.fr/cogui#ct_d7a78641-722f-4609-8f5a-90affc111e00" x="30" y="173"/> 
<concept id="c_669f89c9-eb93-4caa-aa90-f8e31be92245" idType="http://www.lirmm.fr/cogui#ct_d7a78641-722f-4609-8f5a-90affc111e00" x="30" y="293"/> 
<relation id="r_93ecba23-873d-490c-8ce3-40611158006b" idType="http://www.lirmm.fr/cogui#rt_af40394c-9e62-4e92-b05b-352de5db876f" x="45" y="113"/> 
<relation id="r_257f3815-2ac4-4b9e-8b8c-7d9ae6259b52" idType="http://www.lirmm.fr/cogui#rt_af40394c-9e62-4e92-b05b-352de5db876f" x="53" y="233"/> 
<edge cid="c_591883b6-ca82-42ee-bd35-b4ce29ffd286" label="1" rid="r_93ecba23-873d-490c-8ce3-40611158006b"/> 
<edge cid="c_f765dff9-1cd4-42f1-bf95-87cbda00257d" label="2" rid="r_93ecba23-873d-490c-8ce3-40611158006b"/> 
<edge cid="c_f765dff9-1cd4-42f1-bf95-87cbda00257d" label="1" rid="r_257f3815-2ac4-4b9e-8b8c-7d9ae6259b52"/> 
<edge cid="c_669f89c9-eb93-4caa-aa90-f8e31be92245" label="2" rid="r_257f3815-2ac4-4b9e-8b8c-7d9ae6259b52"/> 
</graph> 
</hypt> 
<conc> 
<graph id="_rule1_conc"> 
<concept id="c_fe469224-c26a-49dc-a17a-697faa20aca3" idType="http://www.lirmm.fr/cogui#ct_d7a78641-722f-4609-8f5a-90affc111e00" x="173" y="53"/> 
<concept id="c_656473af-5b41-4ed7-b4fc-be3af9ce544d" idType="http://www.lirmm.fr/cogui#ct_d7a78641-722f-4609-8f5a-90affc111e00" x="180" y="293"/> 
<relation id="r_3b36e657-bc62-4050-baad-c3a04ade3af2" idType="http://www.lirmm.fr/cogui#rt_af40394c-9e62-4e92-b05b-352de5db876f" x="203" y="158"/> 
<edge cid="c_fe469224-c26a-49dc-a17a-697faa20aca3" label="1" rid="r_3b36e657-bc62-4050-baad-c3a04ade3af2"/> 
<edge cid="c_656473af-5b41-4ed7-b4fc-be3af9ce544d" label="2" rid="r_3b36e657-bc62-4050-baad-c3a04ade3af2"/> 
</graph> 
</conc> 
<conPts> 
<couple idC1="c_591883b6-ca82-42ee-bd35-b4ce29ffd286" idC2="c_fe469224-c26a-49dc-a17a-697faa20aca3"/> 
<couple idC1="c_669f89c9-eb93-4caa-aa90-f8e31be92245" idC2="c_656473af-5b41-4ed7-b4fc-be3af9ce544d"/> 
</conPts> 
</rule> 
</cogxml> 

更新 這張照片顯示了我的程序貌似現在。我想在一列中使用概念名稱,在另一列中使用代碼。我無法停止爲每種概念類型顯示相同的代碼。

enter image description here

+0

您提供的腳本不產生例如輸出。 XML file is never loaded('$ name = $ _FILES ['file'] ['name']; $ dom-> load($ name); $ cogxml = simplexml_load_file($ name);') – fusion3k

+0

當您上傳它所做的一個文件。 – Charlotte

+0

不需要,你必須使用'$ _FILES ['file'] ['tmp_name']'。您的工作代碼**必須與示例不同。順便說一句,看到我的答案,瞭解你的主要問題。 – fusion3k

回答

1

僅使用一個DOM foreach()循環外線,因爲ctype數據是從同一個XML源上拉。但是,仍舊沿用前面問題@michi's answer的結構(例如在循環外部初始化dom對象)。

$newdom = new DOMDocument('1.0', 'utf-8'); 
$newdom->formatOutput = true; 
$newdom->preserveWhiteSpace = false; 

$xpath = new DOMXPath($dom); 

<table border=1> 
    <tr><th>Concept Name</th><th>Relation Type</th><th>CoGui XML</th><th>CharGer XML</th></tr> 

    <?php 
    foreach ($dom->getElementsByTagName("ctype") as $ctype) { 
    ?> 
     <tr><td><?php echo $ctype->getAttribute('label'); ?></td><td></td> 

     <?php    
     $newdom->loadXML("<cogxml><support><conceptTypes /><relationTypes /></support></cogxml>");  
     $newnode = $newdom->importNode($ctype, true); 
     $newdom->getElementsByTagName("conceptTypes")->item(0)->appendChild($newnode); 

     $id = $ctype->getAttribute('id'); 

     foreach ($xpath->query("//cogxml/support/conceptTypes/order[@id1='$id']") as $order) { 
      $newnode = $newdom->importNode($order, true); 
      $newdom->getElementsByTagName("conceptTypes")->item(0)->appendChild($newnode); 
     }  

     foreach ($xpath->query("//cogxml/support/relationTypes/rtype[contains(@idSignature, '$id')]") as $rtype) { 
      $newnode = $newdom->importNode($rtype, true); 
      $newdom->getElementsByTagName("relationTypes")->item(0)->appendChild($newnode); 
     }?> 
     <td><xmp><?php echo $newdom->saveXML(); ?></xmp></td><td></td></tr><?php 
    }?> 

</table> 

See eval.in

+0

謝謝,但這也不起作用。它在一行中顯示整個xml文件,而不是一行中的初級員工,下一位員工等。這是我正在嘗試執行的操作,但我無法使其工作。 – Charlotte

+0

哎呦。請參閱編輯。嘗試將最後一行標記''與echo作爲同一行(所以在dom循環內而不是數據循環內)。 – Parfait

+0

好的,這是我以前試過的,但它只顯示一列中的所有內容。它將所有內容放入概念類型列中,而不是將代碼放入CoGui XML列中 – Charlotte

1

你的代碼是一團糟。

第一條規則:將php的辛苦作業與輸出分開。您的HTML的最終輸出是這樣的:

<table border=1> 
    <tr> 
     <th>Concept Name</th><th>Relation Type</th><th>CoGui XML</th><th>CharGer XML</th> 
    </tr> 
    <tr> 
     <td>DATA LABEL</td><td></td><td><xmp>NEWDOM</xmp></td><td></td> 
    </tr> 
     <td><xmp>NEWDOM</xmp></td><td></td></tr> 
     <td><xmp>NEWDOM</xmp></td><td></td></tr> 
     (...) 
</table> 

正如您所看到的,您的表結構已損壞。發生這種情況是因爲您在foreach循環中打開<tr>,並在嵌套的foreach中關閉它。

執行你所有的東西比較/創建新的XML的/ etc ...把​​值多維數組這樣的(而不是直接打印):

$output = 
[ 
    [ 
     'concept' => ... 
     'relation' => ... 
     'cogui' => ... 
     'charger' => ... 
    ], 
    [ 
     'concept' => ... 
     'relation' => ... 
     'cogui' => ... 
     'charger' => ... 
    ], 
    [ ... ]  
] 

然後,清潔你的代碼編寫一個HTML一樣這:

<table border=1> 
    <tr> 
     <th>Concept Name</th> 
     <th>Relation Type</th> 
     <th>CoGui XML</th> 
     <th>CharGer XML</th> 
    </tr> 
    <?php foreach($output as $row): ?> 
    <tr> 
     <td><?php echo $row['concept']; ?></td> 
     <td><?php echo $row['relation']; ?></td> 
     <td><?php echo $row['cogui']; ?></td> 
     <td><?php echo $row['charger']; ?></td> 
    </tr> 
    <?php endforeach; ?> 
</table> 

這將有助於更好地維護您的代碼。

旁註:

<xmp>是一個HTML 3.2的標籤現在已經過時。請考慮使用替代它:

<td><?php echo htmlentities($row['cogui']); ?></td> 

或:

<td><?php highlight_string($row['cogui']); ?></td>