如何通过标签访问XML标签并将值插入相关标签

时间:2018-07-25 06:55:21

标签: php xml web-services

我创建了一个名为AmaSend.xml的xml文件。现在,我已经创建了一个表单以从用户那里获取输入,并且我想将这些值插入该AmaSend.xml文件中。而且我已经成功地将值插入了单个标签。但是,如何将标签中的值插入标签?

我的意思是这样

<userIdentifier>
<originIdentification>
  <sourceOffice>Insert Value 1</sourceOffice>
</originIdentification>
<originatorTypeCode>Insert Value 2</originatorTypeCode>
<originator>Insert value 3</originator>

这是AmaIndex.php文件。

<html>
<head>

</head>

<body>

<?php if (isset($_REQUEST['ok'])) {

    $xml = new DOMDocument("1.0", "UTF-8");
    $xml->load("AmaSend.xml");

    $rootTag = $xml->getElementsByTagName("Security_Authenticate")->item(0);
    $dataTag = $xml->createElement("data");

    $OfficeIdTag = $xml->createElement("sourceOffice", $_REQUEST['OfficeId']);
    $UserIdTag = $xml->createElement("originator", $_REQUEST['UserId']);
    $DutyCode = $xml->createElement("referenceIdentifier", $_REQUEST['DutyCode']);
    $OrganizationTag = $xml->createElement("organizationId", $_REQUEST['Organization']);
    $PasswordTag = $xml->createElement("binaryData", $_REQUEST['Password']);

    $originatorTypeCodeag = $xml->createElement("originatorTypeCode", $_REQUEST['originatorTypeCode']);
    $referenceQualifierTag = $xml->createElement("referenceQualifier", $_REQUEST['referenceQualifier']);
    $dataLengthTag = $xml->createElement("dataLength", $_REQUEST['dataLength']);
    $dataType = $xml->createElement("dataType", $_REQUEST['dataType']);

    $dataTag->appendChild($OfficeIdTag);
    $dataTag->appendChild($UserIdTag);
    $dataTag->appendChild($DutyCode);
    $dataTag->appendChild($OrganizationTag);
    $dataTag->appendChild($PasswordTag);

    $dataTag->appendChild($originatorTypeCodeag);
    $dataTag->appendChild($referenceQualifierTag);
    $dataTag->appendChild($dataLengthTag);
    $dataTag->appendChild($dataType);

    $rootTag->appendChild($dataTag);

    $xml->save("AmaSend.xml");
} ?>

<form action="AmaIndex.php" method="post">

    OfficeId : <input type="text" name="OfficeId"/> <br><br>
    UserId : <input type="text" name="UserId"/> <br><br>
    DutyCode : <input type="text" name="DutyCode"/> <br><br>
    Organization : <input type="text" name="Organization"/> <br><br>
    Password : <input type="text" name="Password"/> <br><br>

    <input type="submit" name="ok"> 

    <input type="hidden" name="originatorTypeCode" value="U"/> <br><br>
    <input type="hidden" name="referenceQualifier" value="DUT"/> <br><br>
    <input type="hidden" name="dataLength" value="8"/> <br><br>
    <input type="hidden" name="dataType" value="E"/> <br><br>

</form>

</body>
</html>

这是AmaSend.xml文件。

<?xml version="1.0"?>
<Security_Authenticate>
  <userIdentifier>
    <originIdentification>
      <sourceOffice>value1</sourceOffice>
    </originIdentification>
    <originatorTypeCode>value2</originatorTypeCode>
    <originator>value3</originator>
  </userIdentifier>
  <dutyCode>
    <dutyCodeDetails>
      <referenceQualifier>value4</referenceQualifier>
      <referenceIdentifier>value5</referenceIdentifier>
    </dutyCodeDetails>
  </dutyCode>
  <systemDetails>
    <organizationDetails>
      <organizationId>value6</organizationId>
    </organizationDetails>
  </systemDetails>
  <passwordInfo>
    <dataLength>value7</dataLength>
    <dataType>value8</dataType>
    <binaryData>value9</binaryData>
  </passwordInfo>
 </Security_Authenticate>

0 个答案:

没有答案