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