Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Changing Node name

843834Oct 15 2007 — edited Oct 15 2007
Hello all,

I am parsing in an XML document that looks like
<transactions>
  <trans>
    <create></create>
    <playerName>Matt</playerName>
    <derivative>
      <name>Oooooh!</name>
      <derivativeType>
        <clauseType>
          <totalLiterals>3</totalLiterals>
          <posLiterals>1</posLiterals>
        </clauseType>
        <clauseType>
          <totalLiterals>1</totalLiterals>
          <posLiterals>0</posLiterals>
        </clauseType>
      </derivativeType>
      <creator>
        <playerName>Matt</playerName>
      </creator>
      <price>
        <money>1.0</money>
      </price>
    </derivative>
  </trans>
</transactions>
What I want to do is rename the <trans> tag to a <createEvent> tag. There may be an easier way to do it than what I am doing but this is what I have so far:
DocumentBuilder db = dbf.newDocumentBuilder();
Document currentPlayersDoc = db.parse(currentPlayerDoneFile);
XPathFactory xFactory = XPathFactory.newInstance();
XPath xpath = xFactory.newXPath();
XPathExpression createNodesExpr = xpath.compile("//trans[create]");
NodeList createNodes = (NodeList)createNodesExpr.evaluate(currentPlayersDoc, XPathConstants.NODESET);
for(int k = 0; k < createNodes.getLength(); k++){
	Node n = createNodes.item(k);
	Node createNode = currentPlayersDoc.importNode(currentPlayersDoc.createElement("create"), true);
        n.getParentNode().replaceChild(createNode, n);
        NodeList oldNodesChildren = n.getChildNodes();
						
        for(int l = 0; l < oldNodesChildren.getLength(); l++){						   createNode.appendChild(oldNodesChildren.item(l));
	}						
						
}
What this results in is:
<?xml version="1.0" encoding="UTF-8"?><transactions>
  <create>
    
    
    
  </create>
</transactions>
Does anyone have any idea why all the child nodes are not showing up? Or does anyone have any suggestions on how to do this in a simpiler way? Thanks
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 12 2007
Added on Oct 15 2007
7 comments
275 views