我一直在用Java编写XML文件。代码执行时没有任何错误,但我试图添加的额外部分未成功添加。
我错过了什么?如何更正我的方法来重写现有文件,并添加我需要的额外组件?
public static synchronized AffLink writeAffLinkEntry(String mPrimSysComp, String mSecSysComp, String mPrimSysCompId,
String mSecSysCompId) throws Exception {
String primSysCompApiKey = RandomStringUtils.random(50);
String secSysCompApiKey = RandomStringUtils.random(50);
//ADD RECORD TO THE XML FILE
File fXmlFile = new File("C:/App/J/AffLinkList.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
Element rootElement = doc.createElement("affiliates");
Element affiliatePairElement = doc.createElement("affiliate_pair");
affiliatePairElement.setAttribute("prim_sys_comp", mPrimSysComp);
affiliatePairElement.setAttribute("sec_sys_comp", mSecSysComp);
affiliatePairElement.setAttribute("prim_sys_comp_id", mPrimSysCompId);
affiliatePairElement.setAttribute("sec_sys_comp_id", mSecSysCompId);
affiliatePairElement.setAttribute("prim_sys_comp_api_key", primSysCompApiKey);
affiliatePairElement.setAttribute("sec_sys_comp_api_key", secSysCompApiKey);
rootElement.appendChild(affiliatePairElement);
// doc.appendChild(rootElement);
try {
Transformer tr = TransformerFactory.newInstance().newTransformer();
tr.setOutputProperty(OutputKeys.INDENT, "yes");
tr.setOutputProperty(OutputKeys.METHOD, "xml");
tr.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
/* tr.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "affiliates.dtd");*/
tr.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
// send DOM to file
tr.transform(new DOMSource(doc),
new StreamResult(new FileOutputStream("C:/App/J/AffLinkList.xml")));
} catch (TransformerException te) {
System.out.println(te.getMessage());
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
//update cache
affLinkList = syncAffLinkList(true);
return new AffLink(mPrimSysComp, mSecSysComp, mPrimSysCompId, mSecSysCompId, primSysCompApiKey, secSysCompApiKey);
}
这是XML文档的开头,但它可能会在以后的内部包含元素。
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<affiliates>
</affiliates>
答案 0 :(得分:1)
我最好的猜测是你需要将根元素添加到xml文档中。
override func supportedInterfaceOrientations() -> Int {
return Int(UIInterfaceOrientationMask.All.rawValue)
}
更新: 好像你试过这个。所以,我不明白你想要实现的目标。
添加根元素对我有用,并给出以下结果。
doc.appendChild(rootElement);
答案 1 :(得分:0)
这是我修改以使代码正常工作
String primSysCompApiKey = RandomStringUtils.random(50, true, true);
String secSysCompApiKey = RandomStringUtils.random(50, true, true);
//ADD RECORD TO THE XML FILE
File fXmlFile = new File("C:/App/Jl/AffLinkList.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
/* doc.getDocumentElement().normalize();*/
Element rootElement = doc.getDocumentElement();
Element affiliatePairElement = doc.createElement("affiliate_pair");
rootElement.appendChild(affiliatePairElement);
Element sysCompElement = doc.createElement("sysComp");
sysCompElement.appendChild((doc.createTextNode(mPrimSysComp)));
affiliatePairElement.appendChild(sysCompElement);
Element sysCompIdElement = doc.createElement("sysCompId");
sysCompIdElement.appendChild((doc.createTextNode(mPrimSysCompId)));
affiliatePairElement.appendChild(sysCompIdElement);
Element affCompElement = doc.createElement("affSysComp");
affCompElement.appendChild((doc.createTextNode(mSecSysComp)));
affiliatePairElement.appendChild(affCompElement);
Element affCompIdElement = doc.createElement("affSysCompId");
affCompIdElement.appendChild((doc.createTextNode(mSecSysCompId)));
affiliatePairElement.appendChild(affCompIdElement);
Element apiKeyInElement = doc.createElement("apiKeyIn");
apiKeyInElement.appendChild((doc.createTextNode(primSysCompApiKey)));
affiliatePairElement.appendChild(apiKeyInElement);
Element apiKeyOutElement = doc.createElement("apiKeyOut");
apiKeyOutElement.appendChild((doc.createTextNode(secSysCompApiKey)));
affiliatePairElement.appendChild(apiKeyOutElement);
try {
Transformer tr = TransformerFactory.newInstance().newTransformer();
tr.setOutputProperty(OutputKeys.INDENT, "yes");
tr.setOutputProperty(OutputKeys.METHOD, "xml");
tr.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
/* tr.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "affiliates.dtd");*/
tr.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
// send DOM to file
tr.transform(new DOMSource(doc),
new StreamResult(new FileOutputStream("C:/App/J/AffLinkList.xml")));
} catch (TransformerException te) {
System.out.println(te.getMessage());
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
//update cache
affLinkList = syncAffLinkList(true);
return new AffLink(mPrimSysComp, mSecSysComp, mPrimSysCompId, mSecSysCompId, primSysCompApiKey, secSysCompApiKey);