Solved Batch file to read some data from XML.

October 19, 2012 at 07:44:11
Specs: Server 2008, 8 gig
I'm trying to make a batch file to pull data out of a file and set it as a variable.

The tricky part is I need to read a XML file, and I only need the data between the quotes of the following line...

narrative="I only need this text here"

The text in that line can also contain spaces, brackets, slashes, dashes and colons.


See More: Batch file to read some data from XML.

Report •

✔ Best Answer
November 8, 2012 at 17:43:50
Yeh, batch can be kind of lame handling xml, but in this case, it seems like it should have worked since findstr was doing the heavy lifting, leaving batch only handling that one line. You might examine your xml file to see what kind of line-breaks it's using - maybe it's cr with no lf. You can use debug or edit /80 to see what it's using.
OR, as per Razor's suggestion, vbscript (or another language) could do a readall and probably solve the problem:
'==== begin vbscript "xmlfind"
if wscript.arguments.count<1 then
wscript.echo "Usage: XMLFIND file.xml targetstring"
wscript.quit
end if
targ=wscript.arguments(1)
set fso=createobject("scripting.filesystemobject")
set test=fso.opentextfile(wscript.arguments(0),1)
z=test.readall
'remove lcase() from foll line to observe case in search
point1=instr(lcase(z),lcase(targ))
if point1=0 then
wscript.echo "NOT FOUND: string: "&targ
wscript.quit
end if
point1=point1+len(targ)
found=ltrim(mid(z,point1))
quote=left(found,1)
found=mid(found,2)
point2=instr(found,quote)
if point2>0 then
found=left(found,point2-1)
end if
wscript.echo chr(34)&found&chr(34)
'===== end vbscript


#1
October 19, 2012 at 08:37:54
@echo off & setlocal enabledelayedexpansion
for /f "delims=" %%a in ('findstr /C:"narrative" nameoffile.xml') do set content=%%a
set content=%content:*"=%
set content=%content:~0,-1%
echo %content%
pause > nul
exit /b

Report •

#2
October 19, 2012 at 21:57:28
@for /f "tokens=1* delims==" %%a in ('findstr /i /c:"narrative=" dilbert.xml') do set xxvar=%%~b
set xxvar
pause > nul
::===== end
assuming that only one instance of "narrative=" exists in the file, otherwise it just gets the last one, but that could be fixed if needed.

Report •

#3
November 8, 2012 at 09:07:06
Both of these answers are giving me the same result. I get all the data in the XML and not just the narrative. Any ideas?

Report •

Related Solutions

#4
November 8, 2012 at 10:37:08
Have you tried a better language? Possibly one with XML support?

How To Ask Questions The Smart Way


Report •

#5
November 8, 2012 at 17:43:50
✔ Best Answer
Yeh, batch can be kind of lame handling xml, but in this case, it seems like it should have worked since findstr was doing the heavy lifting, leaving batch only handling that one line. You might examine your xml file to see what kind of line-breaks it's using - maybe it's cr with no lf. You can use debug or edit /80 to see what it's using.
OR, as per Razor's suggestion, vbscript (or another language) could do a readall and probably solve the problem:
'==== begin vbscript "xmlfind"
if wscript.arguments.count<1 then
wscript.echo "Usage: XMLFIND file.xml targetstring"
wscript.quit
end if
targ=wscript.arguments(1)
set fso=createobject("scripting.filesystemobject")
set test=fso.opentextfile(wscript.arguments(0),1)
z=test.readall
'remove lcase() from foll line to observe case in search
point1=instr(lcase(z),lcase(targ))
if point1=0 then
wscript.echo "NOT FOUND: string: "&targ
wscript.quit
end if
point1=point1+len(targ)
found=ltrim(mid(z,point1))
quote=left(found,1)
found=mid(found,2)
point2=instr(found,quote)
if point2>0 then
found=left(found,point2-1)
end if
wscript.echo chr(34)&found&chr(34)
'===== end vbscript

Report •

#6
November 9, 2012 at 07:45:29
I opened up the XML file in notepad++ and turned on all characters, I was able to see the majority of the XML does not have CR or IF, its mostly one big line.

Running the vbscript works great, but I'm going ot have to start learning vbscripting.

I think I'd like to make something that allowed a command line syntex like this:

VBSCRIPT FILENAME.XML WHATtoFIND TARGETFILE.TXT

Then call VBSCRIPT within my batch, and grab the targetfile.txt for data.


Report •

#7
November 9, 2012 at 10:54:11
That version gives me an error, file not found, line 8, char: 1, code 800a0035

Report •

#8
November 9, 2012 at 10:56:02
I overhauled the basic script (post #5) to be more adaptable and try to do what you said, but it isn't tested yet. As you can see, there is no output file because batch doesn't really need one, but that could be added.
here is how batch could use the script:

for /f "tokens=*" %%a in ('FINDXML.VBS xmlfile "targettag"') do set content=%%~a
echo %content%
::==== end snippet
note that targettag must be quoted in case it contains spaces, and the returned value is also quoted for the same reason. the tilde (%%~a) removes them or you can leave them in.
If you need an output file:
FINDXML.VBS xmlfile "targettag" >outfile

I used both types of quotes for the termination of the content, because i think xml allows either type (like html). "test" and 'test' are both accepted.


Report •

#9
November 9, 2012 at 11:14:12
The VBScript works impressively well, however when calling it from a batch as described above, it still pops the data up in a window.

Running it into an output file as such.

findxml.vbs xmlfile "targettag" >outfile

also has the same result, the data pops up in a window, and odly the outfile is blank?

EDIT: I did find a way to run this to do what I need.

cscript //nologo findxml.vbs xmlfile "targettag" > outfile.txt


Report •

#10
November 9, 2012 at 14:46:59
The XML specification is hard to implement correctly, especially in a script. VBScript already has XML parsing in the form of Msxml2.DOMDocument, so why not let it handle the parsing?

attributeName = "//@" & WScript.Arguments(1)
fullPath = CreateObject("Scripting.FileSystemObject") _
 .GetAbsolutePathName(WScript.Arguments(0))
Set XML = CreateObject("Msxml2.DOMDocument.6.0")
XML.setProperty "SelectionLanguage", "XPath"
XML.setProperty "ProhibitDTD", False
XML.ValidateOnParse = False

If Not XML.Load(fullPath) Then
  WScript.StdErr.WriteLine "Failed to parse: " & fullPath _
   & vbNewLine & XML.parseError.reason
  WScript.Quit 1
End If

For Each node In XML.selectNodes(attributeName)
  WScript.StdOut.WriteLine node.Text
Next 'node

Usage:

cscript //nologo findxml.vbs xmlfile "targettag" >outfile

How To Ask Questions The Smart Way


Report •

Ask Question