0

I need to parse some information from an XML file that does not have any return characters using a batch script. My strategy is to make a copy of the XML file and insert the proper return characters, then parse out the lines I need. The goal is to insert a return character between the '><' characters.

The XML file example:

<NOUNS><thing 1>hammer</thing 1><person>The Stig</person></NOUNS>
5
  • 2
    Nooooooo! You just need to use some scripting that includes XML parsing. No need to worry about return characters. I'd strongly suggest using powershell. Commented Jun 5, 2015 at 19:36
  • Unfortunately, powershell is not an option. If given the option, I would gladly use pretty much any other scripting language to do this. Commented Jun 5, 2015 at 19:49
  • Ok - take a look at the second solution here.. It refers to calling xml.exe from the batch file. In any case, avoid DIY XML parsing :) Commented Jun 5, 2015 at 20:02
  • The xml.exe is good tool to know. However, do to restrictions that got me into this mess, I can't use third party applications. Initially I thought this would not be that difficult to parse with a simple batch function. Thank you for the advise. Commented Jun 5, 2015 at 20:13
  • Why do you need to insert return characters? XML parsers don't care. Commented Jun 6, 2015 at 0:02

2 Answers 2

1

The solution below eliminate fields that include wild card characters * or ?. It may also fail if quotes are included in a field.

@echo off
setlocal EnableDelayedExpansion

set "part="
call :ProcessFile < file.xml > fileWithNL.xml
goto :EOF

:ProcessFile
   set "line="
   set /P "line="
   set "line=!part!!line!"
   if not defined line exit /B

   for %%a in ("!line:><=>" "<!") do (
      set "part=%%~a"
      if "!part:~-1!" equ ">" (
         echo !part!
         set "part="
      )
   )
goto ProcessFile

EDIT: New hybrid method added

The new solution below is a Batch-JScript hybrid script that is more efficient than the former pure Batch solution and have not its limitations. Copy the code in a file with .bat extension.

@set @a=0  /*
@cscript //nologo //E:Jscript "%~F0" < file.xml > fileWithNL.xml
@goto :EOF */

WScript.Stdout.Write(WScript.Stdin.ReadAll().replace(/></g,">\r\n<"));
Sign up to request clarification or add additional context in comments.

Comments

0

save this with .bat extension.It accepts one argument - the xml file:

0</* :
@cscript /nologo /E:jscript "%~f0" %*
@goto :EOF
*/0;

var FSOObj = new ActiveXObject("Scripting.FileSystemObject");
var ARGS = WScript.Arguments;
if (ARGS.Length < 1 ) {
 WScript.Echo("No file passed");
 WScript.Quit(1);
}
var filename=ARGS.Item(0);

var readStream=FSOObj.OpenTextFile(filename, 1);

var content=readStream.ReadAll();
readStream.Close();

function replaceAll(find, replace, str) {
  return str.replace(new RegExp(find, 'g'), replace);
}

var newConten=replaceAll("><",">\r\n<",content);

var writeStream=FSOObj.OpenTextFile(filename, 2);
writeStream.WriteLine(newConten);
writeStream.Close();

2 Comments

The hello label is not needed; the first line can be just 0</* :. It looks more cryptic this way! ;) You should enclose the file name between quotes: "%~f0"
@Aacini - aah yes.I was focused on the jscript part and the pattern was just copy/pasted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.