Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a problem with my batch. XMLStarlet does not show me the complete line.

However, I get with the batch file below only 3DS and the rest of the line is not shown.

Output should be only the version information from first title tag line containing 3DS in XML file, e.g. 9.4.0-21 without 3DS.

I would be very glad about any answer.

My batch file:

@echo off
cls
echo.
echo.
echo ----------- Nintendo 3DS Firmware Checker --------------------------------------
echo --------------------- A CMD based Tool -----------------------------------------
echo.
echo.
echo.
httpget http://yls8.mtheall.com/ninupdates/feed.php nintendo3dsupdate.xml
for /f %%i in ('XML.EXE sel -t -v "//channel/item/title" "nintendo3dsupdate.xml"') do set "var111=%%i" >nul
echo.
echo.
echo                "%var111%" is the currently 3ds firmware.
echo.
echo.
pause
exit

The content of the XML file nintendo3dsupdate.xml:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/">
    <channel>
      <title>Sysupdate Reports</title>
      <atom:link href="http://yls8.mtheall.com/ninupdates/feed.php" rel="self" type="application/rss+xml" />
      <link>http://yls8.mtheall.com/ninupdates/reports.php</link>
      <description>Nintendo System Update Reports</description>
      <lastBuildDate>Thu, 11 Dec 2014 23:05:04 +0000</lastBuildDate>
      <language>en</language>
      <sy:updatePeriod>hourly</sy:updatePeriod>
      <sy:updateFrequency>1</sy:updateFrequency>
        <item>
        <title>3DS 9.4.0-21</title>
        <link><![CDATA[http://yls8.mtheall.com/ninupdates/reports.php?date=12-11-14_07-05-04&sys=ctr]]></link>
        <guid isPermaLink="true"><![CDATA[http://yls8.mtheall.com/ninupdates/reports.php?date=12-11-14_07-05-04&sys=ctr]]></guid>
        <description>3DS 9.4.0-21</description>
        <pubDate>Thu, 11 Dec 2014 23:05:04 +0000</pubDate>
    </item>
    <item>
        <title>3DS 9.3.0-21 (stage5)</title>
        <link><![CDATA[http://yls8.mtheall.com/ninupdates/reports.php?date=12-08-14_07-45-04&sys=ctr]]></link>
        <guid isPermaLink="true"><![CDATA[http://yls8.mtheall.com/ninupdates/reports.php?date=12-08-14_07-45-04&sys=ctr]]></guid>
        <description>3DS 9.3.0-21 (stage5)</description>
        <pubDate>Mon, 08 Dec 2014 23:45:03 +0000</pubDate>
    </item>
    <item>
        <title>3DS 9.3.0-21 (stage4)</title>
        <link><![CDATA[http://yls8.mtheall.com/ninupdates/reports.php?date=12-08-14_07-35-04&sys=ctr]]></link>
        <guid isPermaLink="true"><![CDATA[http://yls8.mtheall.com/ninupdates/reports.php?date=12-08-14_07-35-04&sys=ctr]]></guid>
        <description>3DS 9.3.0-21 (stage4)</description>
        <pubDate>Mon, 08 Dec 2014 23:35:03 +0000</pubDate>
    </item>
    <item>
        <title>3DS 9.3.0-21 (stage3)</title>
        <link><![CDATA[http://yls8.mtheall.com/ninupdates/reports.php?date=12-08-14_07-25-04&sys=ctr]]></link>
        <guid isPermaLink="true"><![CDATA[http://yls8.mtheall.com/ninupdates/reports.php?date=12-08-14_07-25-04&sys=ctr]]></guid>
        <description>3DS 9.3.0-21 (stage3)</description>
        <pubDate>Mon, 08 Dec 2014 23:25:04 +0000</pubDate>
    </item>
 </channel>
</rss>

The indents are with horizontal tab characters. There are 2 tabs left to <title>3DS 9.4.0-21</title>.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
118 views
Welcome To Ask or Share your Answers For Others

1 Answer

Here is the batch code to get version information from first title tag starting with 3DS from XML file:

@echo off
cls
echo.
echo.
echo ----------- Nintendo 3DS Firmware Checker --------------------------------------
echo --------------------- A CMD based Tool -----------------------------------------
echo.
echo.
echo.
del "%TEMP%
intendo3dsupdate.xml">nul 2>nul
set FirmwareVersion3DS=
httpget http://yls8.mtheall.com/ninupdates/feed.php "%TEMP%
intendo3dsupdate.xml"
for /F "usebackq tokens=3 delims=< " %%V in (`%SystemRoot%System32findstr.exe "<title>3DS" "%TEMP%
intendo3dsupdate.xml" 2^>nul`) do (
    set "FirmwareVersion3DS=%%V"
    goto OutputInfo
)
echo Failed to determine version of 3DS firmware!
echo.
goto :EndBatch

:OutputInfo
echo.
echo.
echo                "%FirmwareVersion3DS%" is the current 3DS firmware.
echo.
echo.
set FirmwareVersion3DS=

:EndBatch
del "%TEMP%
intendo3dsupdate.xml">nul 2>nul
pause

The XML file is parsed first by command findstr searching for simple text string <title>3DS returning

        <title>3DS 9.4.0-21</title>
        <title>3DS 9.3.0-21 (stage5)</title>
        <title>3DS 9.3.0-21 (stage4)</title>
        <title>3DS 9.3.0-21 (stage3)</title>

Those 4 lines each indented with 2 tabs are redirected directly to command for which splits each line using left angle bracket and space character as delimiter. Therefore first line is split to:

  • token 1: ???????????????? (two horizontal tabs)
  • token 2: title>3DS
  • token 3: 9.4.0-21
  • token 4: /title>

As just the version number is of interest, just token 3 is assigned to loop variable V assigned next to environment variable FirmwareVersion3DS.

For this task just the first line with a version information is of interest. So loop is exited with a jump to output the found version information after processing the first line returned by command findstr.

An error message is output instead of the version information if an error occurred like no XML file because of no connection to web server, or syntax in XML file changed in the meantime.

There is no check made if token 3 is really referencing the version information. Therefore the batch file code must be updated if for example spaces instead of tabs are output by the PHP script on server for indenting the lines.

The batch code deletes the XML file before downloading it from server to avoid parsing an old XML file. The XML file and the used environment variable are also deleted before exiting the batch file just for leaving a clean environment back.


EDIT:

The line with command for could be also

for /F "usebackq tokens=2 delims=<   " %%V in (`%SystemRoot%System32findstr.exe "<title>3DS" "nintendo3dsupdate.xml" 2^>nul`) do (

There are 3 characters after delims=:

  1. an angle bracket < and
  2. a horizontal tab character (not a sequence of spaces as browser displays) and
  3. a single space character.

The order of the delimiter characters is important as otherwise command line interpreter would exit batch script because of a syntax error.

The indenting tabs are ignored with this modification. Therefore now first line is split to:

  • token 1: title>3DS
  • token 2: 9.4.0-21
  • token 3: /title>

And token 2 instead of 3 contains now the string of interest to output.

This solution is better than first one as it does not matter anymore if the line with the version string is indented with spaces or tabs or not indented at all.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...