SimpleXML: Can't insert child into node with attributes

你。 提交于 2019-12-11 06:54:56

问题


I need to code a web service that creates a FM XML file.

The expected output file looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<FMPXMLRESULT xmlns="http://www.filemaker.com/fmpxmlresult">
  <ERRORCODE>0</ERRORCODE>
  <PRODUCT BUILD="27/11/2002" NAME="FileMaker Pro" VERSION="6.0Dv4"/>
  <DATABASE DATEFORMAT="d.M.yyyy" LAYOUT="" NAME="Schlüssel Adresse für green" RECORDS="3" TIMEFORMAT="k:mm:ss"/>
  <METADATA>
    <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="Datum" TYPE="DATE"/>
    <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="Sprache Typ" TYPE="TEXT"/>
...
 </METADATA>
   <RESULTSET FOUND="3">
    <ROW MODID="0" RECORDID="1">
      <COL>
        <DATA>12.11.2012</DATA>
      </COL>
...
    </ROW>
  </RESULTSET>
</FMPXMLRESULT>

This is my php code:

 $xml = new SimpleXMLElement('<FMPXMLRESULT xmlns="http://www.filemaker.com/fmpxmlresult"></FMPXMLRESULT>');
 $xml->addChild('ERRORCODE',0);
 $xml->addChild('PRODUCT BUILD="27/11/2002" NAME="FileMaker Pro" VERSION="6.0Dv4"');
 $xml->addChild('DATABASE DATEFORMAT="d.M.yyyy" LAYOUT=" " NAME="Schlüssel Adresse für green" RECORDS="3" TIMEFORMAT="k mm ss" ');
 $node = $xml->addChild('METADATA');
 $node->addChild('FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="Datum" TYPE="DATE"');
 $node->addChild('FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="Sprache Typ" TYPE="TEXT"');

 ...

$node = $xml->addChild('RESULTSET FOUND="3"');
$node->addChild('FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="Datum" TYPE="DATE"');

There are two things that I can't get to work:

  • The TIMEFORMAT="k:mm:ss" in the DATABASE node provokes the following error. Removing the colons ':' solves the error message problem but won't produce the same file

    error on line 2 at column 80: error parsing attribute name

  • As soon as there is 'FOUND="3"' AND a child in the RESULTSET, the RESULTSET node provokes an error. It does not cause any error if there is no child. No error if the FOUND="3" is removed.

    This page contains the following errors:

    error on line 2 at column 2199: expected '>'

Any help much appreciated!


回答1:


Solved both cases by using the

 $node->addAttribute("...","...")

method that SimpleXML provides.



来源:https://stackoverflow.com/questions/21378794/simplexml-cant-insert-child-into-node-with-attributes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!