0

我正在尝试复制此 XML:

<?xml version="1.0"?>
<AccessRequest xml:lang="en-US">
  <AccessLicenseNumber>YourLicenseNumber</AccessLicenseNumber>
  <UserId>YourUserID</UserId>
  <Password>YourPassword</Password>
</AccessRequest>
<?xml version="1.0"?>
<AddressValidationRequest xml:lang="en-US">
  <Request>
    <TransactionReference>
      <CustomerContext>Your Test Case Summary Description</CustomerContext>
      <XpciVersion>1.0</XpciVersion>
    </TransactionReference>
    <RequestAction>XAV</RequestAction>
    <RequestOption>3</RequestOption>
  </Request>

  <AddressKeyFormat>
    <AddressLine>AIRWAY ROAD SUITE 7</AddressLine>  
    <PoliticalDivision2>SAN DIEGO</PoliticalDivision2>
    <PoliticalDivision1>CA</PoliticalDivision1>
    <PostcodePrimaryLow>92154</PostcodePrimaryLow>
    <CountryCode>US</CountryCode>
  </AddressKeyFormat>
</AddressValidationRequest>

我正在使用一个类来构建请求:

  public UpsRequestBuilder()
    {
        try
        {
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

            doc = docBuilder.newDocument();
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
    }

    public void accessRequestBuilder(String accessKey, String username, String password)
    {
        Element accessRequest = doc.createElement("AccessRequest");
        doc.appendChild(accessRequest);

        Element license = doc.createElement("AccessLicenseNumber");
        accessRequest.appendChild(license);
        license.setTextContent(accessKey);

        Element userId = doc.createElement("UserId");
        accessRequest.appendChild(userId);
        userId.setTextContent(username);

        Element pass = doc.createElement("Password");
        accessRequest.appendChild(pass);
        pass.setTextContent(password);

        System.out.println("completed Requestbuilder");
    }

    public void addAddress(Address address)
    {
        Element addressKeyFormat = doc.createElement("AddressKeyFormat");
        doc.appendChild(addressKeyFormat);

        Element addressLine = doc.createElement("AddressLine");
        addressKeyFormat.appendChild(addressLine);
        addressLine.setTextContent(address.getState() + ' ' + address.getStreet2());

        Element city = doc.createElement("PoliticalDivision2");
        addressKeyFormat.appendChild(city);
        city.setTextContent(address.getCity());

        Element state = doc.createElement("PoliticalDivision1");
        addressKeyFormat.appendChild(state);
        state.setTextContent(address.getState());

        Element zip = doc.createElement("PostcodePrimaryLow");
        addressKeyFormat.appendChild(zip);
        zip.setTextContent(address.getZip());

        Element country = doc.createElement("CountryCode");
        addressKeyFormat.appendChild(country);
        country.setTextContent(address.getCountry()); 
        System.out.println("completed addAddress");
    }

    public void validateAddressRequest(String customerContextString, String action)
    {

        Element addressValidation = doc.createElement("AddressValidationRequest");
        doc.appendChild(addressValidation);
        Element transactionReference = doc.createElement("TransactionReference");
        addressValidation.appendChild(transactionReference);

        Element customerContext = doc.createElement("CustomerContext");
        Element version = doc.createElement("XpciVersion");
        transactionReference.appendChild(customerContext);
        customerContext.setTextContent(customerContextString); //TODO figure out a way to optionally pass context text
        transactionReference.appendChild(version);
        version.setTextContent("1.0");//change this if the api version changes

        Element requestAction = doc.createElement("RequestAction");
        addressValidation.appendChild(requestAction);
        requestAction.setTextContent(action);
        System.out.println("completed validateAddressRequest");
    }

这是使用它的函数:

public void validateAddress(Address address)
    {
        UpsRequestBuilder request = new UpsRequestBuilder();
        request.accessRequestBuilder(accessKey, username, password);
        request.validateAddressRequest("", "3");
        request.addAddress(address);

        System.out.println(request.toString());

    }

当我尝试从中打印出 XML 时,我收到错误“HIERARCHY_REQUEST_ERR:尝试插入不允许的节点”。当我尝试将 addressValidation 元素添加到文档(doc)时,它发生在 validateAddressRequest 函数中。这是确切的行:

doc.appendChild(addressValidation);

将此元素添加到文档有什么问题?

4

1 回答 1

1

将此元素添加到文档有什么问题?

您正在尝试将其添加到文档的顶层。你不能这样做,因为文档已经有一个根元素。任何 XML 文档都只能有一个根元素。

您在问题顶部显示的 XML 不是单个 XML 文档 - 它是两个。

于 2012-11-25T19:20:23.727 回答