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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀