如何使用HTML从XML发送和接收数据

时间:2017-07-15 05:07:10

标签: php xml wordpress

我的客户希望通过快递服务跟踪他的包裹。 Courier Service公司给我一个XML代码来使用他们的API。我是XML的新手。如何通过表单将跟踪号发送到此XML并获取跟踪详细信息?我想在我的WordPress网站上使用它。提前致谢。鉴于代码如下:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.wall);
    initialize();
    Bundle gotBasket = getIntent().getExtras();
    baseurl = gotBasket.getString("url");
    title = gotBasket.getString("title");
    description = gotBasket.getString("des");
    imgsrc = gotBasket.getString("img");
    info = baseurl +"\n" + title + "\n" + description;
    content.setText(info);
}

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
public void onClick(View v){
    String cntnt = content.getText().toString();
    wall.addView(createNewTextView(cntnt));
    content.setText("");
    scroll.fullScroll(ScrollView.FOCUS_DOWN);
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
private TextView createNewTextView(String text) {
    final LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    textView = new TextView(this);
    int id = View.generateViewId();
    textView.setId(id);
    lparams.gravity = Gravity.RIGHT;
    lparams.setMargins(200,10,10,0);
    textView.setLayoutParams(lparams);
    textView.setTextSize(30);
    textView.setBackgroundColor(Color.WHITE);
    textView.setText(text);
    Linkify.addLinks(textView, Linkify.WEB_URLS);
    GradientDrawable gd = new GradientDrawable();
    gd.setShape(GradientDrawable.RECTANGLE);
    gd.setColor(Color.WHITE);
    gd.setStroke(2, Color.BLACK);
    gd.setCornerRadius(20.0f);
    textView.setBackground(gd);
    textView.setPadding(10,10,10,10);
    textView.setGravity(Gravity.LEFT);
    new FetchImageAsyncTask().execute();
    return textView;
}

private void initialize() {
    wall = (LinearLayout) findViewById(R.id.wall);
    content = (EditText) findViewById(R.id.content);
    post = (ImageButton) findViewById(R.id.post);
    scroll = (ScrollView) findViewById(R.id.scroll);
    post.setOnClickListener(this);
}
@RequiresApi(api = Build.VERSION_CODES.CUPCAKE)
public class FetchImageAsyncTask extends AsyncTask<Void, Void, Void> {

    Drawable d;
    @Override
    protected Void doInBackground(Void... params) {
        try {
            //imgsrc = "//upload.wikimedia.org/wikipedia/commons/thumb/3/3a/Aishwarya_Rai_Cannes_2017.jpg/220px-Aishwarya_Rai_Cannes_2017.jpg";
            URL aURL = new URL("https://upload.wikimedia.org/wikipedia/commons/thumb/3/3a/Aishwarya_Rai_Cannes_2017.jpg/220px-Aishwarya_Rai_Cannes_2017.jpg");
            URLConnection conn = aURL.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();
    /* Buffered is always good for a performance plus. */
            BufferedInputStream bis = new BufferedInputStream(is);
    /* Decode url-data to a bitmap. */
            Bitmap bm = BitmapFactory.decodeStream(bis);
            bis.close();
            is.close();

            d =new BitmapDrawable(bm);
            //image = Drawable.createFromStream((InputStream)new URL("https://en.wikipedia.org/wiki/File:Aishwarya_Rai_Cannes_2017.jpg").getContent(), "src");
        } catch (IOException e) {
            Log.e("DEBUGTAG", "Remote Image Exception", e);
        }

        return null;
    }

    protected void onPostExecute(Long result) {
        textView.setCompoundDrawables(null, null, null, d);
    }
}

1 个答案:

答案 0 :(得分:0)

显然有2个部分,需要输入客户详细信息和参考号以及接收的发送,然后显示详细信息。

第一部分可能是......

$xmlStr =<<< XML
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <GetCNDetailsByReferenceNumber xmlns="http://IP_ADDRESS/">
      <userName></userName>
      <password></password>
      <customerReferenceNo></customerReferenceNo>
    </GetCNDetailsByReferenceNumber>
  </soap12:Body>
</soap12:Envelope>
XML;

$xml= simplexml_load_string($xmlStr);
$xml->registerXPathNamespace("default", "http://IP_ADDRESS/");
$details = $xml->xpath("//default:GetCNDetailsByReferenceNumber");
$details[0]->userName="SomeName";
$details[0]->password="password";
$details[0]->customerReferenceNo="Custref";
echo 'Send: ' . $xml->asXML();

所有这一切都在探索价值观,当然你需要为你的特定请求提供详细信息,但这会为你提供想法和XML,然后你可以将它发送给他们的服务。

一旦返回值被返回,那么它只是提取数据并显示它......

$receiveXML = <<< XML
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <GetCNDetailsByReferenceNumberResponse xmlns="http://IP_ADDRESS">
      <GetCNDetailsByReferenceNumberResult>xmlxml</GetCNDetailsByReferenceNumberResult>
    </GetCNDetailsByReferenceNumberResponse>
  </soap12:Body>
</soap12:Envelope>
XML;

$xml= simplexml_load_string($receiveXML);
$xml->registerXPathNamespace("default", "http://IP_ADDRESS");
$details = $xml->xpath("//default:GetCNDetailsByReferenceNumberResult");
echo "\n\nReturn value:".(string)$details[0];

如何获取信息取决于他们的API,但这将获取该数据并提取内容。

一个问题......当我用原始XML尝试它时,我发现xmlns="IP_ADDRESS/"变体引起了警告(不是绝对路径),因此我将其更改为{{{ 1}}。您可能需要更改此值并使用相同的值更新xmlns="http://IP_ADDRESS/"行。