Avaliação do Usuário

Estrela inativaEstrela inativaEstrela inativaEstrela inativaEstrela inativa
 

Imgine que por algum motivo qualquer você precise processar um arquivo XML a partir da linha de comandos do Linux, no meu caso um Debian, mas quase que não importa.

 

 

Ao invés de ficar se "matando" muito (você vai ter que se "matar" um pouco) com comandos como grep ou cut, para facilitar sua vida você pode usar o xml2.

 

 

Para instalar:

 

 

{code}

apt-get install xml2

{/code}

 

 

O jeito de usar é:

 

 

{code}

xml2 < arquivo.xml

{/code}

 

 

Esse programa tem como objetivo converter um arquivo XML em uma saída texto plana onde as tags XML são convertidas em caminhos semelhantes à diretórios. Isso que facilita bastante na hora de procurar e encontrar dados dentro do arquivo usando comandos do Shell.

 

 

Abaixo um exemplo de arquivo XML obtido pela API do Google Maps e logo depois o resultado depois de convertido pelo XML2.

 

 

Exemplo:

 

 

{code}

<GeocodeResponse>

<status>OK</status>

<result>

<type>locality</type>

<type>political</type>

<formatted_address>Americana - SP, Brasil</formatted_address>

<address_component>

<long_name>Americana</long_name>

<short_name>Americana</short_name>

<type>locality</type>

<type>political</type>

</address_component>

<address_component>

<long_name>Americana</long_name>

<short_name>Americana</short_name>

<type>administrative_area_level_2</type>

<type>political</type>

</address_component>

<address_component>

<long_name>São Paulo</long_name>

<short_name>SP</short_name>

<type>administrative_area_level_1</type>

<type>political</type>

</address_component>

<address_component>

<long_name>Brasil</long_name>

<short_name>BR</short_name>

<type>country</type>

<type>political</type>

</address_component>

<geometry>

<location>

<lat>-22.7378463</lat>

<lng>-47.3335688</lng>

</location>

<location_type>APPROXIMATE</location_type>

<viewport>

<southwest>

<lat>-22.7846095</lat>

<lng>-47.3876380</lng>

</southwest>

<northeast>

<lat>-22.6804504</lat>

<lng>-47.2160317</lng>

</northeast>

</viewport>

<bounds>

<southwest>

<lat>-22.7846095</lat>

<lng>-47.3876380</lng>

</southwest>

<northeast>

<lat>-22.6804504</lat>

<lng>-47.2160317</lng>

</northeast>

</bounds>

</geometry>

<partial_match>true</partial_match>

<place_id>ChIJxfZsL6uQyJQRqM2OSWKw1LI</place_id>

</result>

</GeocodeResponse>

{/code}

 

 

Resultado:

 

 

{code}

/GeocodeResponse/status=OK

/GeocodeResponse/result/type=locality

/GeocodeResponse/result/type

/GeocodeResponse/result/type=political

/GeocodeResponse/result/formatted_address=Americana - SP, Brasil

/GeocodeResponse/result/address_component/long_name=Americana

/GeocodeResponse/result/address_component/short_name=Americana

/GeocodeResponse/result/address_component/type=locality

/GeocodeResponse/result/address_component/type

/GeocodeResponse/result/address_component/type=political

/GeocodeResponse/result/address_component

/GeocodeResponse/result/address_component/long_name=Americana

/GeocodeResponse/result/address_component/short_name=Americana

/GeocodeResponse/result/address_component/type=administrative_area_level_2

/GeocodeResponse/result/address_component/type

/GeocodeResponse/result/address_component/type=political

/GeocodeResponse/result/address_component

/GeocodeResponse/result/address_component/long_name=SãPaulo

/GeocodeResponse/result/address_component/short_name=SP

/GeocodeResponse/result/address_component/type=administrative_area_level_1

/GeocodeResponse/result/address_component/type

/GeocodeResponse/result/address_component/type=political

/GeocodeResponse/result/address_component

/GeocodeResponse/result/address_component/long_name=Brasil

/GeocodeResponse/result/address_component/short_name=BR

/GeocodeResponse/result/address_component/type=country

/GeocodeResponse/result/address_component/type

/GeocodeResponse/result/address_component/type=political

/GeocodeResponse/result/geometry/location/lat=-22.7378463

/GeocodeResponse/result/geometry/location/lng=-47.3335688

/GeocodeResponse/result/geometry/location_type=APPROXIMATE

/GeocodeResponse/result/geometry/viewport/southwest/lat=-22.7846095

/GeocodeResponse/result/geometry/viewport/southwest/lng=-47.3876380

/GeocodeResponse/result/geometry/viewport/northeast/lat=-22.6804504

/GeocodeResponse/result/geometry/viewport/northeast/lng=-47.2160317

/GeocodeResponse/result/geometry/bounds/southwest/lat=-22.7846095

/GeocodeResponse/result/geometry/bounds/southwest/lng=-47.3876380

/GeocodeResponse/result/geometry/bounds/northeast/lat=-22.6804504

/GeocodeResponse/result/geometry/bounds/northeast/lng=-47.2160317

/GeocodeResponse/result/partial_match=true

/GeocodeResponse/result/place_id=ChIJxfZsL6uQyJQRqM2OSWKw1LI

{/code}