Created
June 3, 2014 11:27
-
-
Save pewerner/903d959a49c363a3fa8c to your computer and use it in GitHub Desktop.
JS XML Parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var indent_level = 0 | |
function indent() | |
{ | |
var spaces = "" | |
for( var i=0; i<indent_level; ++i) | |
spaces = spaces + " " | |
return spaces | |
} | |
function get_attributes( node,output,is_root) | |
{ | |
indent_level++ | |
var node_name = node.get( "nodeName") | |
var attributes = node.get( "attributes") | |
var length = attributes.get( "length") | |
if( is_root) node_name = "root" | |
for( var i = 0; i < length; ++i) | |
{ | |
var attr = attributes.get( "item", i) | |
var attr_name = attr.get( "nodeName") | |
var attr_value = attr.get( "nodeValue") | |
output[ attr_name] = attr_value | |
} | |
indent_level-- | |
} | |
function recurse_children( node, output) | |
{ | |
indent_level++ | |
var child_nodes = node.get( "childNodes") | |
var length = child_nodes.get( "length") | |
for( var i = 0; i < length; ++i) | |
{ | |
var c_node = child_nodes.get( "item", i) | |
var c_name = c_node.get( "nodeName") | |
var c_index = 0 | |
// add text nodes as value of parent node (output) | |
if( c_name == '#text') | |
{ | |
output._value_ = c_node.get( "nodeValue") | |
} | |
// skip comment nodes and other crap | |
if( c_name[0] == '#') continue | |
if( output[ c_name] == undefined){ | |
output[ c_name] = [] | |
} | |
var index = output[ c_name].length | |
output[ c_name][index] = [] | |
get_attributes( c_node, output[ c_name][index] ) | |
recurse_children( c_node, output[ c_name][index] ) | |
} | |
indent_level-- | |
} | |
function getNodeValue( node, searchValue){ | |
var child_nodes = node.getElementsByTagName( searchValue) | |
var first_element = child_nodes.get( "item",0) | |
var first_element_text = first_element.get( "text") | |
//print ("first_element_text = " + first_element_text) | |
return first_element_text | |
} | |
function ParseXML( file_name, root) | |
{ | |
var file = new File() | |
file.Open( file_name) | |
var text = file.Read() | |
var doc = new ActiveX( "Microsoft.XMLDOM") | |
doc.loadXML( text) | |
var root_node = doc.get( "documentElement") | |
recurse_children( root_node, root) | |
doc.close; | |
} | |
// This is just a print function to show the output | |
// after compressing all of the data into array format | |
function recurse_nodes( node, display) | |
{ | |
for( x in node){ | |
new_display = display + x | |
if( node[x] != '') | |
print( new_display + " = " + node[x] + "\n") | |
recurse_nodes( node[x], new_display + ".") | |
} | |
} | |
function TestParser(file_path, printToLog) | |
{ | |
// Initialize the "root" array | |
var root = new Array(); | |
ParseXML( file_path, root) | |
// After all of the parsing pass root into the print function | |
if (printToLog) | |
recurse_nodes( root, "root.") | |
// examples of how to get values out of the hash | |
print( "*_*_*_*_*_*_*_*_*_*_*_*_*_*" + "\n"); | |
return root; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var root = new Array(); | |
ParseXML(working_input_file, root) | |
var numProjects = root["project"].length |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | |
<run id="13AM1202050" attribute_1="none"> | |
<project seq="1" id="13AMBC799"> | |
<l_plate id="A13BEE11213465"/> | |
<e_block id="M13AMZ12025001"/> | |
<e_block id="M13AMZ12025002"/> | |
</project> | |
<project seq="2" id="13AMBC797"> | |
<l_plate id ="A13BEE11213480"/> | |
<e_block id="M13AMZ12025002" /> | |
<e_block id="M13AMZ12025001" /> | |
<e_block id="M13AMZ12025002" /> | |
<e_block id="M13AMZ12025001" /> | |
</project> | |
<project seq="3" id="13AMBC796"> | |
<l_plate id="A13BEE11213502" /> | |
<e_block id="M13AMZ12025002" /> | |
<e_block id="M13AMZ12025001" /> | |
<e_block id="M13AMZ12025002" /> | |
<e_block id="M13AMZ12025001" /> | |
<e_block id="M13AMZ12025002" /> | |
</project> | |
</run> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment