Parsing xml file

In the VASL module, we use xml files to store data which is read at start up and is used to configure maps (amongst other things).

At present, we have a process that takes a plain text file written as xml and opens it as a java Inputsteam, which is then converted to an org.w3c.dom.Document using VASSAL.build.Builder.createDocument(). The resulting Document is then parsed into two org.w3c.dom.NodeLists. The key part of the code is shown below:

public SSRControlsFile(InputStream SSRControlsFile, String archiveName) throws IOException {

    Logger logger = LoggerFactory.getLogger(SSRControlsFile.class);


    try {
        Document doc = Builder.createDocument(SSRControlsFile);
        if(doc != null) {
            basicNodes = doc.getElementsByTagName(BASIC_ELEMENT_NAME);
            optionNodes = doc.getElementsByTagName(OPTION_ELEMENT_NAME);
        }
    }
    catch (Exception e) {
        logger.warn("Unable to read SSR control file in archive " + archiveName);
        logger.warn(e.getMessage());
    }
}

My issue is that, for a variety of reasons, I wish to move the data currently stored in xml notation in a plain text file into a larger xml file and retrieve it from there and send it through the code shown above to generate the two NodeLists. I cannot get this to work. However I try to extract the particular part of the xml file that I need, it does not produce the same basicNodes, optionNodes output that I require.

The plain text file contains just one top level xml element, called SSRControls. In the xml file to which I need to add this SSRControls element, it becomes one of anywhere from 3 -10 same level elements under the root element, structured something like this (edited for brevity):

<?xml version="1.0"?>

    <buildingTypes>

    	
</buildingTypes>

<overlaySSRules>
    	<overlaySSRule name="Snow" image="RB_Ground_Snow.gif" x="0" y="0" />
	<overlaySSRule name="GF1" image="RB_Gutted_O10.gif" x="790" y="609" />
	<overlaySSRule name="GF2" image="RB_Gutted_O40.gif" x="789" y="2527" />
	<overlaySSRule name="GF3" image="RB_Gutted_J43.gif" x="508" y="2761" />
	<overlaySSRule name="GF4" image="RB_Gutted_S43.gif" x="1014" y="2713" />
	<overlaySSRule name="GF5" image="RB_Gutted_P21.gif" x="844" y="1351" />
</overlaySSRules>

<colors>
	<color name="WoodenBldg1" red="255" green="255" blue="255" terrain="Wooden Building" elevation="1" />
	<color name="WoodenBldg1" red="200" green="200" blue="200" terrain="Wooden Building" elevation="0" />
	<color name="WoodenBldg2" red="110" green="110" blue="110" terrain="Wooden Building, 1 Level" elevation="1" />
	<color name="StoneBldg1" red="110" green="110" blue="110" terrain="Stone Building, 1 Level" elevation="1" />
	<color name="StoneBldg3" red="255" green="255" blue="255" terrain="Stone Building" elevation="1" />
</colors>

<colorSSRules>

</colorSSRules>

<SSRControls>
	<Option name="RB Terrain Transformations">
		<Checkbox name="">
			<entry name="Ground Snow" rule="Snow" text="Ground Snow"/>
		</Checkbox>

		<Checkbox name="">
			<entry name="O10 is gutted (GF1)" rule="GF1" text="O10 is gutted (GF1)"/>
		</Checkbox>

		<Checkbox name="">
			<entry name="O40 is gutted (GF2)" rule="GF2" text="O40 is gutted (GF2)"/>
		</Checkbox>

		<Checkbox name="">
			<entry name="J43 is gutted (GF3)" rule="GF3" text="J43 is gutted (GF3)"/>
		</Checkbox>

		<Checkbox name="">
			<entry name="S43 is gutted (GF4)" rule="GF4" text="S43 is gutted (GF4)"/>
		</Checkbox>

		<Checkbox name="">
			<entry name="P21 is gutted (GF5)" rule="GF5" text="P21 is gutted (GF5)"/>
		</Checkbox>
	</Option>
</SSRControls>

It is quite correct for some elements to contain no further information.

How best to extract the element from the larger XML file and then feed it into the conversion/parsing process? Do I start right at the beginning and convert the SSRControls element to and InputStream? Or can I extract it as XML and then parse the XML into the two NodeLists?

I have tried the second option using the following lines of code :

org.w3c.dom.Document doc = Builder.createDocument(metadata);
// the root element will be the boardMetadata element
org.w3c.dom.Element root = doc.getDocumentElement();
if (root.getTagName().equals(“boardMetadata”)){
parseSSRControls(root.getElementsByTagName(“SSRControls”));
}
The metadata variable passed to createDocument is an InputStream of the xml file.

The Document variable “doc” produced by the current code and my revisions appears to have the same structure but of course the one produced by my code contains all the other elements besides SSRControls. Is there a way to edit the doc variable?

The parseSSRControls() method works but the NodeLists it produces are not exactly the same as those produced by the code snippet shown earlier. Those NodeLists are used elsewhere in the VASL code and must match the ones produced by our current code.

Any help gratefully received. I realize this is more of a java code problem than a VASSAL issue.

I’m not clear about what you’re trying to accomplish. Is what you want to load a fragment of an XML file?

I’m not clear about what you’re trying to accomplish. Is what you want to load a fragment of an XML file?<

Yes.

I would not try to combine XML fragments from disparate files into one Document for processing, as Document is not designed to work that way and on top of that is quite heavyweight.

What I’d do instead is successively open each file as a Document and process what you need into whatever format you need.