【问题标题】:While (web services netbeans)而(Web 服务 netbeans)
【发布时间】:2016-06-25 15:06:30
【问题描述】:

我有这个方法,我必须从同一个客户中删除所有订单。这是我要从中删除的 .xml 文件

<encomendas>
   <encomenda cod="enc4" data="2016-04-23" cliente="cli5" status="pendente">
      <enc quant="3">9789722523288</enc>
      <enc quant="1">9789726656272</enc>
   </encomenda>
   <encomenda cod="enc5" data="2016-05-16" cliente="cli7" status="pendente">
      <enc quant="1">9789720043702</enc>
      <enc quant="1">9789724146348</enc>
      <enc quant="2">9789724121390</enc>
      <enc quant="1">9789720046451</enc>
   </encomenda>
   <encomenda cod="enc6" data="2016-04-23" cliente="cli5" status="pendente">
      <enc quant="3">9789722523288</enc>
      <enc quant="1">9789726656272</enc>
   </encomenda> 
</encomendas>

这是我制作的代码

    @WebMethod(operationName = "removeorder2")
public String removeorder2(@WebParam(name = "cliente") String id) {
    URL u = this.getClass().getResource("orders.xml");
    Document doc = XMLJDomFunctions.lerDocumentoXML(u.getFile());
    Element encomendas = doc.getRootElement();
    for (int i = 0; i < encomendas.getChildren().size(); i++) {
        Element filho = encomendas.getChildren().get(i);
        if (filho.getAttribute("cliente").getValue().equals(id)) {
            encomendas.getChildren().remove(i);
            XMLJDomFunctions.escreverDocumentoParaFicheiro(doc, u.getFile());
            return "order removed";
        }

    }
    return "order doesnt exist";
}

但是像这样我只在插入要删除的客户端时删除一个。 例如,如果我插入 cli5,它将删除他找到的第一个 cli5。我做了一个 cicle,但我不知道怎么做,我是做一个 xpath 计算我有多少客户还是有简单的方法来做?

【问题讨论】:

    标签: java xml web-services netbeans


    【解决方案1】:

    这是因为当你删除某些东西时,你会立即返回方法。您可以将return "order removed"; 移出 for 循环,并使用类似标志变量的内容来检查是否删除了某些内容。

    @WebMethod(operationName = "removeorder2")
    public String removeorder2(@WebParam(name = "cliente") String id) {
        boolean check = false;
        URL u = this.getClass().getResource("orders.xml");
        Document doc = XMLJDomFunctions.lerDocumentoXML(u.getFile());
        Element encomendas = doc.getRootElement();
        for (int i = 0; i < encomendas.getChildren().size(); i++) {
            Element filho = encomendas.getChildren().get(i);
            if (filho.getAttribute("cliente").getValue().equals(id)) {
                encomendas.getChildren().remove(i);
                XMLJDomFunctions.escreverDocumentoParaFicheiro(doc, u.getFile());
                check = true;
            }
    
        }
        if (check) return "order removed";
        return "order doesnt exist";
    }
    

    另一件事是关于删除逻辑。假设您删除了索引 1 处的项目,因此下一个项目的索引为 1,对吧。但是在接下来的交互中,你去索引 2,而现在在索引 1 的第三项会被跳过吗?所以我猜你必须重新检查下一项的索引,并确保你没有跳过任何内容。

    顺便说一句,escreverDocumentoParaFicheiro的方法是什么?

    【讨论】:

    • 我不认为我理解你的问题......但它有效!谢谢你的帮助。
    • 方法escreverDocumentoParaFicheiro是改文件。例如在文件中你有 cli5 cli5 cli7 cli5 如果你消除 cli5 该方法会将更改写入文件中,因此它只会出现 cli7。
    • 嗯,你仍然需要重新检查下一个循环的索引。假设您有 4 个项目,a、b、b、c,并且您想删除所有 b 项目,对。在索引 1 处找到第一个 b,将其删除。那么会发生什么:第二个 b 的索引现在是索引 1。但是因为您已经更新了循环,所以您将检查索引 2 并在那里找到 c。因此将第二个 b 留在索引 1 处,不会被删除。
    • 我在check = true; 之后放了一个i--;,它成功了!如果我这样离开它会一直有效吗?
    • 当然可以。还有一件事,将行 XMLJDomFunctions.escreverDocumentoParaFicheiro(doc, u.getFile()); 移到循环之外的某个地方。您不想在每个循环中都写入文件吗?只需在代码末尾写一次,或者最好只在有变化时才写。
    猜你喜欢
    • 2012-09-10
    • 1970-01-01
    • 2016-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多