duankange2433 2014-07-22 14:03
浏览 39
已采纳

如何将PHP网页的内容保存到XML文件?

I currently have code that generates XML code for all of the contacts within my Oracle array. I have tried to use the built in XML methods within PHP, but it did not give me the amount of control I need to get the right schema. Thus far, I have been running the PHP file which echoes all the XML code to a webpage and then selecting all within the page, pasting it to a blank Notepad file and saving it as XML. This works perfectly for now, but essentially I would like to be able to run the PHP file and end up with a saved XML file with a dynamic naming convention (writing_request_yyyymmddtttt.xml). Is there any way to easily accomplish this to make the process more automated? Here is a snippet of the code:

echo    htmlspecialchars('<contact contactID="'.$row["UNIQUE ID"].'" action="AddOrModify">')."<br>".
    htmlspecialchars('<contactField name="LastName">'.$row["LAST NAME"].'</contactField>')."<br>".
    htmlspecialchars('<contactField name="FirstName">'.$row["FIRST NAME"].'</contactField>')."<br>".
    htmlspecialchars('<contactField name="MiddleName">'.$row["MIDDLE INITIAL"].'</contactField>')."<br>".
    htmlspecialchars('<contactField name="Country">'.$row["COUNTRY"].'</contactField>')."<br>".
    htmlspecialchars('<contactField name="Code">'.$row["CODE"].'</contactField>')."<br>".
    htmlspecialchars('<contactField name="CustomField" customName="'.$row["CUSTOM LABEL 1"].'">'.$row["CUSTOM VALUE 1"].'</contactField>')."<br>".
    htmlspecialchars('<contactField name="CustomField" customName="'.$row["CUSTOM LABEL 2"].'">'.$row["CUSTOM VALUE 2"].'</contactField>')."<br>".
    htmlspecialchars('<contactField name="CustomField" customName="'.$row["CUSTOM LABEL 3"].'">'.$row["CUSTOM VALUE 3"].'</contactField>')."<br>".
    htmlspecialchars('<contactField name="CustomField" customName="'.$row["CUSTOM LABEL 4"].'">'.$row["CUSTOM VALUE 4"].'</contactField>')."<br>".
    htmlspecialchars('<contactField name="CustomField" customName="'.$row["CUSTOM LABEL 5"].'">'.$row["CUSTOM VALUE 5"].'</contactField>')."<br>".
    htmlspecialchars('<contactField name="CustomField" customName="'.$row["CUSTOM LABEL 6"].'">'.$row["CUSTOM VALUE 6"].'</contactField>')."<br>".
    htmlspecialchars('<contactField name="CustomField" customName="'.$row["CUSTOM LABEL 7"].'">'.$row["CUSTOM VALUE 7"].'</contactField>')."<br>".
    htmlspecialchars('<contactField name="CustomField" customName="'.$row["CUSTOM LABEL 8"].'">'.$row["CUSTOM VALUE 8"].'</contactField>')."<br><br>".


    htmlspecialchars('<contactPointList>')."<br>".

    htmlspecialchars('<contactPoint type="Voice">')."<br>".
    htmlspecialchars('<contactPointField name="Label">'.$row["PHONE LABEL 1"].'</contactPointField>')."<br>".
    htmlspecialchars('<contactPointField name="CountryCode">'.$row["PHONE 1 COUNTRY CODE"].'</contactPointField>')."<br>".
    htmlspecialchars('<contactPointField name="Number">'.$row["PHONE 1"].'</contactPointField>')."<br>".
    htmlspecialchars('<contactPointField name="Extension">'.$row["PHONE EXTENSION 1"].'</contactPointField>')."<br>".
    htmlspecialchars('</contactPoint>')."<br><br>".

    htmlspecialchars('<contactPoint type="Voice">')."<br>".
    htmlspecialchars('<contactPointField name="Label">'.$row["PHONE 2 LABEL"].'</contactPointField>')."<br>".
    htmlspecialchars('<contactPointField name="CountryCode">'.$row["PHONE 2 COUNTRY CODE"].'</contactPointField>')."<br>".
    htmlspecialchars('<contactPointField name="Number">'.$row["PHONE 2"].'</contactPointField>')."<br>".
    htmlspecialchars('<contactPointField name="Extension">'.$row["PHONE EXTENSION 2"].'</contactPointField>')."<br>".
    htmlspecialchars('</contactPoint>')."<br><br>".

    htmlspecialchars('<contactPoint type="Email">')."<br>".
    htmlspecialchars('<contactPointField name="Label">'.$row["EMAIL LABEL 1"].'</contactPointField>')."<br>".
    htmlspecialchars('<contactPointField name="Address">'.$row["EMAIL 1"].'</contactPointField>')."<br>".
    htmlspecialchars('</contactPoint>')."<br><br>".
    htmlspecialchars('</contactPointList>')."<br><br>".
    htmlspecialchars('</contact>')."<br><br>";

This is being run within the oci_fetch_array loop that also validates all the data. Anyone got any ideas?

  • 写回答

1条回答 默认 最新

  • dongtan1009 2014-07-22 14:23
    关注

    Instead of using echo, save the content a variable and then create a new file with fopen() and write to it with fwrite().

    http://php.net/manual/en/function.fwrite.php http://php.net/manual/en/function.fopen.php

    I think you should not use <br> in your code, it seems like you want to do a line break, use " " instead or " " on Windows, PHP_EOL is another option as well.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习残差模块模型
  • ¥20 两个不同Subnet的点对点连接
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)