2016-10-12 6 views
1

おはようございます!テキストファイルからXML?

私は以下のフォーマットに従ったテキストファイルを選択してxmlファイルに変換できるPHPスクリプトを作成中です。

テキストファイル

Barcode:       9091 
Update Bib Status:     Unable to update bib record: item barcode not found. 
Update Holding Status:    Unable to update holdings record: item barcode not found. 
Update Item Status:    Unable to update item record: item barcode not found. 

Item ID:       int 
Barcode:       int 
Title:        some stuff 
Enum/Chron:       
Call Number:      alphanumeric stuff 
Call Number Prefix:     
Holding Location:     some stuff 
Permanent Location:    some stuff 
Temporary Location:    some stuff 
Permanent Type:     some stuff 
Temporary Type:      
Media Type:       
Item Status:      Not Charged 
Statistical Categories:    
Magnetic Media:     No 
Sensitize:       Yes 

私は左の列に情報を取得し、XML要素とコンテンツとして右列の項目としてそれらを使用したいと思います。スヨン....うち、このようなXMLファイルになり、私はスクリプトが実行された後achiveしたい置く:私は、アップロードするファイルを取得することができています

<item> 
<barcode>9091</barcode> 
<ItemID>1234</ItemID> 
<title>In the heart of the Sea</title> 
.... 
</item> 

、しかし私は、作成時に立ち往生しています所望の要素。これはでテキストファイルを分割

$xml = new SimpleXMLElement('<xml/>'); 

foreach (exploe("\n", $data) as $row) 
{ 

    $rowData = explode(":", $row); 
    $xml->addChild(trim($rowData[0]), trim($rowData[1])); 

} 

echo "<pre>"; 
print_r($xml->asXML()); 
echo "</pre>"; 

:あなたの基本的なループはこのようになります

現在のPHPコード

if(isset($_POST["submit"])){ 
    $target_dir = "uploads/"; 
    $target_file = $target_dir . basename($_FILES["scans"]["name"]); //retain file after upload 
    $uploadOk=1; 
    move_uploaded_file($_FILES["scans"]["tmp_name"], $target_file); 
    echo "Your file has been successfully uploaded.<br>";//let user know file has been received. 
    $data=file_get_contents($target_file); //open uploaded file... 
    for ($i=0; $i<$data; i++){ 
     list($k, $v) = explode(":", $data); //split list into key value pairs 
    } 
    $xml= new XMLWriter("<holding></holding>");//start new xml 
+2

試しましたか? –

+0

txtをxmlに変換しようとしたところでコードを共有してください。 –

+0

申し訳ありません。私は途中で提出しました。戻って編集しました。 – Duffman

答えて

0

:私は今のところ以下でき

それぞれの行が途切れていて、各行の最初のコロンのノード名がノード名として使用され、最初のコロンi各行をノード値として(より正確には、同じ行に存在する場合は最初のコロンと2番目のコロンの間にあります。あなたはそれらを自分で処理する必要があります)。私はそこにあなたの空白を殺すトリムを追加しました。

関連する問題