Hi, i have some stuff represented as a DOM tree, lets call this resources.
In this tree are some things i want, one is a list of IP addresses, another is a list of attributes.
What i am trying to do is take the attributes from the original Document and create a new document with these items and then store this document in a HashTable where the IP address is the key.
Ok, so thats the goal, you can forget about the hashtable, etc. I have the following code which is giving me the following error, i'm new to DOM so don't know how to do what i'm trying. anyone who knows how to create a Document from another (i dont want to have to traverse right into the depths of the tree as determening the exact structure of the parts i want and then recreating them would take too long, i just want to grab everything in there, regardless of structure)
CODE
//parse SLA for monitorableAttributes
InputSource input = new InputSource(new StringReader(SLA));
String query = "jsdl:Resources";
XPathHelper xph = new XPathHelper();
NodeList resources = xph.processXPath(query, input);
NodeList monitorableSLAAttributes = null;
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
Node anotherNode = null;
// Create from whole cloth
Element root = (Element)document.createElement("Attributes");
document.appendChild(root);
for (int i = 3; i< resources.item(0).getChildNodes().getLength(); i=i+2) {
anotherNode = resources.item(0).getChildNodes().item(i).cloneNode(true);
root.appendChild(anotherNode);
System.out.println("Attribute["+i+"]: "+anotherNode.getNodeName()+", "+anotherNode.getNodeValue());
}
monitorableSLAAttributes = document.getChildNodes();
for (int i = 0; i<monitorableSLAAttributes.getLength(); i++) {
System.out.println("monitorableSLAattributes["+i+"]: "+monitorableSLAAttributes.item(i).getNodeName()+", "+monitorableSLAAttributes.item(i).getNodeValue());
}
}
catch (ParserConfigurationException pce) {
// Parser with specified options can't be built
pce.printStackTrace();
}
//Parse Resource IP's from SLA
//resources.item(0).getFirstChild().getNodeName();// == jsdl:Resources
//String hostName1 = resources.item(0).getChildNodes().item(0).getNodeName(); == White Space!
//below line grabs jsdl:CandidateHosts
Node candidateHostsNode = resources.item(0).getChildNodes().item(1);
//String hostName2 = candidateHostsNode.getNodeName();
Node childNode = null;
for (int i = 1; i< candidateHostsNode.getChildNodes().getLength();i=i+2) {
//search candidate hosts for all IPs of resources (jsdl:HostName's)
childNode = candidateHostsNode.getChildNodes().item(i);
System.out.println("Found Resource("+i+"): "+childNode.getTextContent()+".");
Registry.getInstance().addMonitoredResource(childNode.getTextContent(), monitorableSLAAttributes);
}
current error:
StartMonitoring(): Exception: WRONG_DOCUMENT_ERR: A node is used in a different document than the one that created it.
org.w3c.dom.DOMException: WRONG_DOCUMENT_ERR: A node is used in a different document than the one that created it.
at org.apache.xerces.dom.ParentNode.internalInsertBefore(Unknown Source)
at org.apache.xerces.dom.ParentNode.insertBefore(Unknown Source)
at org.apache.xerces.dom.NodeImpl.appendChild(Unknown Source)
at eu.sormaproject.sla.SlaEnf.startMonitoring(SlaEnf.java:132)
at eu.sormaproject.sla.SlaEnf.startMonitoring(SlaEnf.java:99)