0

I am getting this error when I am trying to parse XML from batch Script

 error : 
 < was unexpected at this time.

xml:

<driver type=".dbdriver">
        <attributes>localhost;1521;XE;false</attributes>
        <driverType>Oracle thin</driverType>
      </driver>
      <password>7ECE6B7E7D2AF514C55BAE8B3A6B51E7</password>
      <user>JR</user>

batch scrpit:

for /f "tokens=3 delims=><" %%j in ('type %SETTINGSPATH% ^| find "<user>"') do set user=%%j

This code is supposed to read user value from XML which is just "JR" and on some machines I am getting this values; but some machines are not showing this value and showing this error.

Please guide.

2 Answers 2

2

Parsing XML with batch is often problematic and always risky. A valid XML documented could be legitamately reformatted in any number of ways that would break you parser. But if you really want to continue to use batch...

That error message occurs when you have an unescaped and unquoted < character in your IN() clause. The "<user>" is already quoted, so that normally should not be a problem. The problem must stem from the value contained in %SETTINGSPATH%. Either the value must have an unquoted and unescaped <, or there must be an odd number of quotes in the value. The odd number of quotes would cause the <user> to no longer be quoted.

The only other possibility is that you have not shown us all your code, and the error is occuring someplace else.

Sign up to request clarification or add additional context in comments.

Comments

0

This will never work reliably. The reason for this is that you are trying to process Xml using wrong tools. There is an infinite number of textual representations of an Xml document that have the same semantical meaning. As a result a space here or a new line there will not change the semantics of your document but will break your script even though all the tools that process the input as Xml will continue to work correctly. Use PowerShell or vbscript/jscript where you can use Xml capabilities or you will always have problems like this since you should not use a brush to drive screws.

3 Comments

‘This will never work reliably’ – While the OP's experience does seem to confirm your statement, it would be much better if you elaborated on why this is unreliable.
I agree that batch should not be used to parse XML. But I don't see how the XML could cause the reported error.
@AndriyM - added more explanation.

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.