Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,335 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,828 people online right now. Registration is fast and FREE... Join Now!




DOM - Document Object Model help needed

 
Reply to this topicStart new topic

DOM - Document Object Model help needed, creating a new document from parts of an old one

bstdnator
3 Sep, 2008 - 04:06 AM
Post #1

New D.I.C Head
*

Joined: 5 Nov, 2007
Posts: 36


My Contributions
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)
User is offlineProfile CardPM
+Quote Post

bstdnator
RE: DOM - Document Object Model Help Needed
3 Sep, 2008 - 06:11 AM
Post #2

New D.I.C Head
*

Joined: 5 Nov, 2007
Posts: 36


My Contributions
Solved:

CODE

attributeNode = resources.item(0).getChildNodes().item(i).cloneNode(true);
document.adoptNode(attributeNode);
root.appendChild(attributeNode);

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 07:59AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month