Click to See Complete Forum and Search --> : Read a xml file and write into a txt file


basanta007
02-26-2009, 12:42 AM
I have the following xml file structure and need to read the testname,outcome,info,resultname and outcome value. These values are write to a txt file.
I am relatively new to VC++, and want to use the MSXML parser, to read the xml file and write this value in a txt file.

<?xml version="1.0" encoding="UTF-8" ?>
- <tests outcome="PASS">
- <test name="MAIN_DOCUMENT" outcome="PASS">
- <result name="META_HTTP_EQUIV-2" outcome="WARN">
<info>WARN: A matching HTTP response header (content-type) exists but its value differs from the content attribute value</info>
</result>
</test>
<test name="AUTO_REFRESH" outcome="PASS" />
- <test name="CACHING" outcome="PASS">
- <result name="CACHING-5" outcome="WARN">
<info>WARN: The "Expires" header contains a date in the past</info>
</result>
</test>
<test name="CHARACTER_ENCODING_SUPPORT" outcome="PASS" />
- <test name="CONTENT_FORMAT_SUPPORT" outcome="PASS">
- <result name="CONTENT_FORMAT_SUPPORT-2" outcome="WARN">
<info>WARN: The document is served as "application/vnd.wap.xhtml+xml" instead of the recommended "application/xhtml+xml"</info>
</result>
</test>
- <test name="DEFAULT_INPUT_MODE" outcome="PASS">
- <result name="DEFAULT_INPUT_MODE-2" outcome="WARN">
<info>WARN: There is no inputmode attribute on this text entry element</info>
<code><input class="textinput" id="query" maxlength="2048" name="q" size="20" type="text" value=""/></code>
</result>
</test>
<test name="EXTERNAL_RESOURCES" outcome="PASS" />
- <test name="GRAPHICS_FOR_SPACING" outcome="PASS">
- <result name="GRAPHICS_FOR_SPACING-1" outcome="WARN">
<info>WARN: There is a small fully transparent image</info>
</result>
</test>
<test name="IMAGE_MAPS" outcome="PASS" />
<test name="IMAGES_SPECIFY_SIZE" outcome="PASS" />
- <test name="LINK_TARGET_FORMAT" outcome="PASS">
- <result name="LINK_TARGET_FORMAT-2" outcome="WARN">
<info>WARN: The linked resource http://services.google.com/surveys/mobile_search is served with a character encoding ("") that may not be appropriate for a mobile device</info>
</result>
- <result name="LINK_TARGET_FORMAT-2" outcome="WARN">
<info>WARN: The linked resource http://www.google.co.in/webhp?hl=en&output=html is served with a character encoding (iso-8859-1) that may not be appropriate for a mobile device</info>
</result>
</test>


Please share some source code to achive this goal.


Thanks,
Basanta007

Charles
02-26-2009, 10:50 AM
I don't know about VC++ but here's a full featured and functional Windows Script Host / JScript version:<?xml version="1.0" encoding="iso-8859-2"?>
<job>
<runtime>
<description>
Transforms test results file or files from XML to plain text.
</description>
<unnamed name="in" helpstring="XML File to process" type="string" required="true" many="true"/>
<named name="out" helpstring="Output file name" type="string" required="false" many="false"/>
<example>
Examples:
cscript test.wsf test.xml
cscript test.wsf test.xml > test.txt
cscript.test.wsf test.xml -out:test.txt
cscript test.wsf test1.xml test2.xml test3.xml
</example>
</runtime>

<script language="JScript">
<![CDATA[
if (!WScript.arguments.unnamed.length) {
WScript.arguments.showUsage();
WScript.quit();
}

fs = new ActiveXObject ('Scripting.FilesystemObject');
out = WScript.arguments.named ('out') ? fs.openTextFile (WScript.arguments.named ('out'), 2, true, -1) : WScript.stdOut;

function runFile (fileName) {
var doc = new ActiveXObject("msxml2.DOMDocument.3.0");
doc.async = false;
doc.resolveExternals = false;
doc.validateOnParse = false;
doc.load (fileName);
if (doc.parseError.errorCode != 0) {
WScript.stdErr.writeLine ('Error parsing "' + fileName + '". ' + doc.parseError.reason);
return;
}

var e = new Enumerator (doc.getElementsByTagName ('test'));
while (!e.atEnd()) {
out.writeLine ('Test Name: ' + e.item().getAttribute('name'));
out.writeLine ('Test Outcome: ' + e.item().getAttribute('outcome'));

var e1 = new Enumerator (e.item().getElementsByTagName ('result'));
while (!e1.atEnd()) {
out.writeLine ('Result Name: ' + e1.item().getAttribute ('name'));
out.writeLine ('Result Outcome: ' + e.item().getAttribute('outcome'));

var e2 = new Enumerator (e1.item().getElementsByTagName ('info'));
while (!e2.atEnd()) {
out.writeLine ('Info :' + e2.item().firstChild.data);
e2.moveNext();
}

e1.moveNext();
}

out.writeLine()
e.moveNext();
}

}

e = new Enumerator (WScript.arguments.unnamed);
while (!e.atEnd()) {
runFile (e.item());
e.moveNext();
}

]]>
</script>
</job>

zahidraf
03-02-2009, 12:54 AM
Thank you for sharing this


One Button Controller (http://www.zahipedia.com/2008/10/08/one-button-controller/)