gpt4 book ai didi

Java在线程中解析和更新xml值

转载 作者:行者123 更新时间:2023-11-29 08:19:29 27 4
gpt4 key购买 nike

我写了一个多线程程序,每个线程都需要解析一个xml文件并更新新值。问题:假设我有 ABC 的 xml 内容,现在线程 A 解析 xml 并将其更新为 ABCA,同时线程 B 也解析 xml(内容为 ABC)并将其更新为 ABCB。线程 B 在线程 A 更新后更新 xml,所以 xml 的结果是 ABCB,我想要的是 xml 结果应该是 ABCAB。有什么想法可以控制线程中解析和更新的方式吗?这是我的代码:

WSthread.java

public class WSthread extends Thread {
public String WSname;
Process proc= null;
WebServicesXML xml;
WSthread(String name){
WSname=name;
}

public void run() {
try {
//my code
// Run a java app in a separate system process
String cmd = (WSname);
proc = Runtime.getRuntime().exec("java -jar "+cmd);
xml = new WebServicesXML();
//Process proc = Runtime.getRuntime().exec("java -jar .jar");
// Then retreive the process output
//InputStream in = proc.getInputStream();
//InputStream err = proc.getErrorStream();

BufferedReader is = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line;
String regex = "\\bhttp\\b";
Pattern pattern = Pattern.compile(regex);
String WSaddress = "";
while ((line = is.readLine()) != null){
Matcher matcher = pattern.matcher(line);
if(matcher.find()){
WSaddress = line;
System.out.println("Updating WS address..."+WSaddress);
xml.create(WSname, WSaddress);
}
System.out.println(line);
}

}catch(Exception e){
System.out.println(e.getMessage());
}
}
public void close(){
proc.destroy();
}

WebServicesXML.java

public class WebServicesXML{
public int totalWebServices;
public String WSnamelist[];
public synchronized void create(String WSname, String WSaddress) throws OException, TransformerConfigurationException, TransformerException, ParserConfigurationException{
try {

DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbfac.newDocumentBuilder();

Document readdoc = docBuilder.parse("webservices.xml");
// normalize text representation
readdoc.getDocumentElement ().normalize ();
Node rootList = readdoc.getDocumentElement();
Element rootElement = (Element)rootList;

Element webservice = readdoc.createElement("WebService");

Element name = readdoc.createElement("name");
Element address = readdoc.createElement("address");

name.setTextContent(WSname);
address.setTextContent(WSaddress);

webservice.appendChild(name);
webservice.appendChild(address);

rootElement.appendChild(webservice);
/////////////////
//Output the XML

//set up a transformer
TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = transfac.newTransformer();
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
trans.setOutputProperty(OutputKeys.INDENT, "yes");

//create string from xml tree
File file = new File("webservices.xml");
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(file);
DOMSource source = new DOMSource(readdoc);
trans.transform(source, result);

}
catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

最佳答案

基本问题是在每个线程中构建一个单独的 DOM 以应用单个转换。这意味着最后一个运行的线程在将其内容写回 XML 文件方面“获胜”。

一方面您正在使用线程,我假设是为了性能,但另一方面您多次解析和序列化 XML。而且线程实现是不安全的。

我的建议是删除线程并在循环中进行更改。当它工作时,您可以衡量性能,然后选择查看使用线程的实现。

请记住,过早的优化是万恶之源。

关于Java在线程中解析和更新xml值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1388833/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com