Get XML tag data

Asked

Viewed 299 times

4

I have an XML with the following format:

<dados-cad>      
<nome>Wender</nome>
      <data>2805094</data>
      <code>311</code>
</dados-cad>

How do I in PHP to get only the content that is inside each tags above.

  • 1

    Do you already have something? If you publish your attempts you can help better.

2 answers

6

You can use the class SimpleXml php.

Here is a simple example of how to use the class.

<?php

    $xml = '<dados-cad><nome>Wender</nome><data>2805094</data><code>311</code></dados-cad>';

    $simpleXml = simplexml_load_string($xml);

    // Acessando os elementos

    echo $simpleXml->nome;
    echo $simpleXml->data;

More examples of the basic use of this class here.

-1

I know almost nothing about HTML/XML.

Could someone help make this kind of query up using C#?

I use Visual Studio 2017, I’m assembling a client register in windows-Forms.

This time, when xml is sub-level, I have difficulty getting the content from the higher/higher level (I don’t know the term).

The example below refers to a webservic for Cpf query:

    <consulta>
      <status>1</status>
      <return>OK</return>
      <result>
        <numero_de_cpf>000.000.000-00</numero_de_cpf>
        <nome_da_pf>FULANO DE TAL</nome_da_pf>
        <data_nascimento>30/12/1981</data_nascimento>
        <situacao_cadastral>REGULAR</situacao_cadastral>
        <data_inscricao>05/05/2000</data_inscricao>
        <digito_verificador>00</digito_verificador>
        <comprovante_emitido>9F73.DBF8.4F85.7860</comprovante_emitido>
        <comprovante_emitido_data>12:21:27 às 29/08/2017</comprovante_emitido_data>
      </result>
    </consulta>

What I need is to take the contents of the tag 'name_da_pf' and play it in a windows-form.

I am using the code below, but always gives some reference error in xml positioning.

    private void button1_validar_cpf_Click(object sender, EventArgs e)
    {
        string urlConsulta = "http://ws.umsitequalquer.com.br/cpf/?xml5&cpf=@cpf&data=@dt_nasc&token=12545269WrhPkfgDHe11972584";
        DataSet dsRetornaEndereco = new DataSet();
        dsRetornaEndereco.ReadXml(urlConsulta.Replace("@cpf", maskedTextBox1_cpf.Text));
        dsRetornaEndereco.ReadXml(urlConsulta.Replace("@dt_nasc", maskedTextBox1_dt_nasc.Text));
        string retorno = dsRetornaEndereco.Tables[0].Rows[0]["status"].ToString();
        if (retorno == "false")
        {
            MessageBox.Show(this, "CPF Inválido!", "Validação CPF", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        else
        {
            textBox1_nome.Text = dsRetornaEndereco.Tables[0].Rows[0]["nome_da_pf"].ToString();
        }
    }

After typing an invalid Cpf, all right, the application returns the invalid Cpf message, but when I launch a valid Cpf, Visual S. says:

Message: Exception Without Treatment

System.Argumentexception: 'The column 'name_da_pf' does not belong to the query table.'

I’m splitting my head rsrsrs

From now on I thank you all!

Vlw!

  • 1

    You cannot post questions in another question comment. You have to create a new question by entering the tags correctly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.