Lendo arquivos .xml e gravando as informações em um arquivo .txt

Lendo arquivos .xml e gravando as informações em um arquivo .txt

Quero baixar arquivos de escoamento de rio (.xml) de um ChartServer online e criar um arquivo .txt com as informações executando um script .sh. Mas estou tendo dificuldade em obter os dados no formato correto.

Adicionei parte do meu script na esperança de que alguém possa me indicar a direção certa sobre como fazer isso funcionar.

url="http://h-web01.nve.no/ChartServer/ShowData.aspx?req=getchart&ver=1.0&time=-10;0&vfmt=xml&chd=ds=htsr,rt=1,da=18,id=700.2.2.1001.0"

xmllint --xpath '//SeriesData/Serie/Point/Value' ${url} | tr '</Value>' '\n' | grep -v '^$' > value_2.2.txt
xmllint --xpath '//SeriesData/Serie/Point/DateTime' ${url} | tr '</DateTime>' '\n' | grep -v '^$' > datetime.txt

if [ -s datetime.txt ]; then
    while true; do
      read month     || break
      read day       || break
      read year      || break
      read hour      || break
      echo ${year} ${month} ${day} >> date_2.2.txt
done < datetime.txt

# Put the date and runoff file together
while read Q <&3 && read y m d <&4; do
    echo ${y} ${m} ${d} ${Q} >> runoff_2.2.txt
done 3<value_${fra}.${til}.txt 4<date_2.2.txt

Quero que o arquivo .txt contenha:

yyyy month day value

No entanto, meu código fornece apenas valuepara todos os outros "DateTime":

yyyy hh:mm:ss month day value
yyyy hh:mm:ss month day 
yyyy hh:mm:ss month day value
...

Dados de origem

<?xml version="1.0" encoding="utf-8"?>
<SeriesData>
  <Serie>
    <Legend>Glomma med kystområder (700.2.2), Vannføring (m³/s)</Legend>
    <Point>
      <DateTime>03/07/2020 12:00:00</DateTime>
      <Value>673.2365</Value>
    </Point>
    <Point>
      <DateTime>03/08/2020 12:00:00</DateTime>
      <Value>695.2465</Value>
    </Point>
    <Point>
      <DateTime>03/09/2020 12:00:00</DateTime>
      <Value>786.8168</Value>
    </Point>
    <Point>
      <DateTime>03/10/2020 12:00:00</DateTime>
      <Value>766.8459</Value>
    </Point>
    <Point>
      <DateTime>03/11/2020 12:00:00</DateTime>
      <Value>758.2921</Value>
    </Point>
    <!-- ...more data... -->
    <Point>
      <DateTime>03/16/2020 12:00:00</DateTime>
      <Value>702.8088</Value>
    </Point>
    <Statistics/>
  </Serie>
</SeriesData>

Responder1

Se você não se importa em usar xmlstarletem vez de xmllintvocê pode usar isso

url="http://h-web01.nve.no/ChartServer/ShowData.aspx?req=getchart&ver=1.0&time=-10;0&vfmt=xml&chd=ds=htsr,rt=1,da=18,id=700.2.2.1001.0"
curl --silent "$url" |
    xmlstarlet sel -t -m '//SeriesData/Serie/Point' -v 'concat(substring(DateTime,7,4)," ",substring(DateTime,1,2)," ",substring(DateTime,4,2)," ",Value)' -n

Infelizmente, o <DateTime/>elemento não é uma data XML adequada, por isso temos que dividi-lo manualmente, em vez de usar funções de processamento de data XPath.

Saída

2020 03 07 673.2365
2020 03 08 695.2465
2020 03 09 786.8168
2020 03 10 766.8459
2020 03 11 758.2921
...
2020 03 16 702.8088

informação relacionada