DomDocument-没有结束标签?

时间:2018-11-17 20:17:19

标签: php xml domdocument php-5.6

这是我第一次处理DomDocument和PHP。我快完成了,但是不知何故XML缺少结束标记。我在下面的XML中缺少“运动”的结束标记。

<?xml version="1.0" encoding="UTF-8"?>
<Persons PageCount="1" PageIndex="1" ApplicationId="secret" xmlSchemaVersion="1.0" GenerationDate="2018-11-17T23:10:31" IsIncremental="true">
  <Person CreateDate="2008-08-29T08:39:44" LMDate="2012-08-29T08:39:44" OptionType="Update">
    <PersonId>82</PersonId>
    <PersonalNumber>19660903-xxxx</PersonalNumber>
    <FirstName>Joakim</FirstName>
    <LastName>Krassman</LastName>
    <BirthDate>1966-09-03</BirthDate>
    <Gender>male</Gender>
    <Organisations>
      <Organisation OptionType="Update">
        <OrgID>31215</OrgID>
        <OrganisationRF_NR>45264</OrganisationRF_NR>
        <OrganisationNumber>556717xxxx</OrganisationNumber>
        <Sports>
          <Sport OptionType="Update"/>
          <SportID>43</SportID>
          <IsActive LMDate="2012-08-29T08:39:44">true</IsActive>
        </Sports>
      </Organisation>
    </Organisations>
  </Person>
</Persons>

这是我的PHP代码:

<?php
$generate_date = date('Y-m-d\TH:i:s');

$doc = new DOMDocument('1.0', 'UTF-8'); 
$doc->formatOutput = true;

/* PERSONS */ 
$r = $doc->createElement( "Persons" );
$attr = $doc->createAttribute('PageCount');
$attrVal = $doc->createTextNode(1);
$attr->appendChild($attrVal);
$r->appendChild($attr);


$attr = $doc->createAttribute('PageIndex');
$attrVal = $doc->createTextNode(1);
$attr->appendChild($attrVal);
$r->appendChild($attr);

$attr = $doc->createAttribute('ApplicationId');
$attrVal = $doc->createTextNode("secret");
$attr->appendChild($attrVal);
$r->appendChild($attr);

$attr = $doc->createAttribute('xmlSchemaVersion');
$attrVal = $doc->createTextNode("1.0");
$attr->appendChild($attrVal);
$r->appendChild($attr);


$attr = $doc->createAttribute('GenerationDate');
$attrVal = $doc->createTextNode($generate_date);
$attr->appendChild($attrVal);
$r->appendChild($attr);

$attr = $doc->createAttribute('IsIncremental');
$attrVal = $doc->createTextNode('true');
$attr->appendChild($attrVal);
$r->appendChild($attr);


$doc->appendChild( $r );

/* PERSON */


$b = $doc->createElement( "Person" );
$attr = $doc->createAttribute('CreateDate');
$attrVal = $doc->createTextNode('2008-08-29T08:39:44');
$attr->appendChild($attrVal);
$b->appendChild($attr);

$attr = $doc->createAttribute('LMDate');
$attrVal = $doc->createTextNode('2012-08-29T08:39:44');
$attr->appendChild($attrVal);
$b->appendChild($attr);

$attr = $doc->createAttribute('OptionType');
$attrVal = $doc->createTextNode('Update');
$attr->appendChild($attrVal);
$b->appendChild($attr);


/* START PERSON SINGULAR */

$person = $doc->createElement( "PersonId" );
$person->appendChild(
$doc->createTextNode( 82 )
);
$b->appendChild( $person );

$person = $doc->createElement( "PersonalNumber" );
$person->appendChild(
$doc->createTextNode( '19660903-xxxx' )
);
$b->appendChild( $person );

$person = $doc->createElement( "FirstName" );
$person->appendChild(
$doc->createTextNode( 'Joakim' )
);
$b->appendChild( $person );

$person = $doc->createElement( "LastName" );
$person->appendChild(
$doc->createTextNode( 'Krassman' )
);
$b->appendChild( $person );

$person = $doc->createElement( "BirthDate" );
$person->appendChild(
$doc->createTextNode( '1966-09-03' )
);
$b->appendChild( $person );


$person = $doc->createElement( "Gender" );
$person->appendChild(
$doc->createTextNode( 'male' )
);
$b->appendChild( $person );

    /* SKJUT IN ORGINISATION */ 
     $org = $doc->createElement( "Organisations" );
     $b->appendChild($org);
     $subOrg = $doc->createElement( "Organisation" );


     $subOrg->setAttribute("OptionType", "Update");
     $org->appendChild( $subOrg );


     $orgDet = $doc->createElement( "OrgID" );
     $orgDet->appendChild(
     $doc->createTextNode( 31215 ));
     $subOrg->appendChild( $orgDet );

     $orgDet = $doc->createElement( "OrganisationRF_NR" );
     $orgDet->appendChild(
     $doc->createTextNode( 45264 ));
     $subOrg->appendChild( $orgDet );

     $orgDet = $doc->createElement( "OrganisationNumber" );
     $orgDet->appendChild(
     $doc->createTextNode( 556717xxxx ));
     $subOrg->appendChild( $orgDet );

     /* SPORTS */

     $sport = $doc->createElement( "Sports" );
     $subOrg->appendChild($sport);
     $subOrg = $doc->createElement( "Sport" );
     $subOrg->setAttribute("OptionType", "Update");
     $sport->appendChild( $subOrg );

     $sportDet = $doc->createElement( "SportID" );
     $sportDet->appendChild(
     $doc->createTextNode( 43 ));
     $sport->appendChild( $sportDet );

     $sportDet = $doc->createElement( "IsActive" );
     $sportDet->setAttribute("LMDate", "2012-08-29T08:39:44");

     $sportDet->appendChild(
     $doc->createTextNode( 'true' ));

     $sport->appendChild( $sportDet );


    /* SLUT ORGINISATION */


/* SLUT PERSON SINGULAR*/
$r->appendChild( $b );


echo ($doc->saveXML());
?>

我确定自己在做一些根本性的错误,有人可以指导我解决问题吗?

1 个答案:

答案 0 :(得分:1)

标签是一个空标签,因此在标签(/>)的末尾关闭-

<Sport OptionType="Update"/>

如果您需要关闭标签,请使用LIBXML_NOEMPTYTAG ...

更改保存。
echo ($doc->saveXML(null, LIBXML_NOEMPTYTAG));

哪个会给你...

<Sport OptionType="Update"></Sport>

如果您需要结构...

  <Sport OptionType="Update">
    <SportID>43</SportID>
    <IsActive LMDate="2012-08-29T08:39:44">true</IsActive>
  </Sport>

然后输入代码...

 $subOrg = $doc->createElement( "Sport" );
 $subOrg->setAttribute("OptionType", "Update");
 $sport->appendChild( $subOrg );

 $sportDet = $doc->createElement( "SportID" );
 $sportDet->appendChild(
 $doc->createTextNode( 43 ));
 $sport->appendChild( $sportDet );

 $sportDet = $doc->createElement( "IsActive" );
 $sportDet->setAttribute("LMDate", "2012-08-29T08:39:44");

 $sportDet->appendChild(
 $doc->createTextNode( 'true' ));

 $sport->appendChild( $sportDet );

您需要更改两行

$sport->appendChild( $sportDet );

相反,将它们添加到$subOrg ...

$subOrg->appendChild( $sportDet );