大家早上好!
我正在处理PHP脚本,允许用户选择符合以下格式的文本文件并将其转换为xml文件。
文字档案
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元素,将右栏中的项目用作内容。 Soo ....我希望在运行脚本后输出的输出将是一个像这样的xml文件:
<item>
<barcode>9091</barcode>
<ItemID>1234</ItemID>
<title>In the heart of the Sea</title>
....
</item>
我能够上传文件,但是我仍然坚持创建所需的元素。
到目前为止我的内容如下:
当前的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
答案 0 :(得分:0)
你的基本循环看起来像这样:
$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>";
这会在每个换行符处拆分文本文件,然后使用每行中第一个冒号左边的任何内容作为节点名称,并将每行中第一个冒号的任何内容作为节点值(或者更多)确切地说,无论第一个冒号和第二个冒号之间是否存在,如果它存在于同一行;您需要自己处理这些冒号。我在那里添加了一个修剪来杀死你的空白。