如果条件不适用||操作者

时间:2014-10-13 16:45:16

标签: java xml

我已经编写了java代码来从xml标签中获取节点,并且基于该节点,它应该被删除。对于相同条件的某些情况它工作正常并且它对某些情况不起作用。我一直试图找到一个解决方案但无法解决。 Plz帮助我们.Below是我写的代码

import java.io.FileInputStream;
import java.io.FileOutputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class CL_RGTIS_CREMAS_DWM_FILT {

    /**
     * @param args
     */
    public static void main(String[] args) 
    {
        CL_RGTIS_CREMAS_DWM_FILT domParse=new CL_RGTIS_CREMAS_DWM_FILT();
        try
        {
        FileInputStream in=new FileInputStream("C:/Users/Home/Downloads/Test1.xml");        
        FileOutputStream out = new FileOutputStream("C:/Users/Home/Downloads/out.xml");
        domParse.execute(in,out);   
        }
        catch(Exception e)
        {
            System.out.println("catch exception"+e.getMessage());

        }

    }
    public void execute(FileInputStream in,FileOutputStream out)
    {
        try
        {
            TransformerFactory  transformerFactory=TransformerFactory.newInstance();
            Transformer transformer=transformerFactory.newTransformer();

            DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
            //ignore whitespace within document
            factory.setIgnoringElementContentWhitespace(true);
            /*parser is aware of name space*/
            factory.setNamespaceAware(true);

            DocumentBuilder builderel=factory.newDocumentBuilder();
            Document  docIn=builderel.parse(in);
            Node root, headerItem = null,childItem=null,childItem1=null;
            NodeList id, headerNodes = null,childNodes=null,childNodes1=null;

            id = docIn.getElementsByTagName("IDOC");
            for (int g =0; g< id.getLength(); g++)
            {

                root = id.item(g);          
                headerNodes = root.getChildNodes();


                for(int j =0; j<headerNodes.getLength(); j++)
                {   
                    headerItem = headerNodes.item(j);
                    childNodes=headerItem.getChildNodes();

                    for(int k=0;k<childNodes.getLength();k++)
                    {
                        childItem=childNodes.item(k);
                        childNodes1=childItem.getChildNodes();


                        if((childItem.getNodeName().equalsIgnoreCase("E1LFB1M"))||(childItem.getNodeName().equalsIgnoreCase("E1LFBKM")))
                        {
                            childItem.getParentNode().removeChild(childItem);
                            System.out.println("in if stmt");



                        }

                    }



                }

            }
            DOMSource dom = new DOMSource(docIn);
            StreamResult result = new StreamResult(out);
            transformer.transform(dom, result);
        }
        catch(Exception e)
        {
            System.out.println("execute method"+e.getMessage());
        }
    }

}

2 个答案:

答案 0 :(得分:0)

在k循环中,代码删除了k th childItem。我不是最微妙的想法,NodeList是否由实际的DOM节点支持。您可以尝试递减k,这样您就不会无意中跳过for k++上的下一个项目。

在每种情况下,当k递减时,k < childNodes.getLength()将迭代少一次。

         childItem.getParentNode().removeChild(childItem);
         --k;

当然,更便携,更安全的解决方案应该更好;在循环内收集要删除的项目列表。

答案 1 :(得分:0)

假设有6个标签带有E1LfBKM标签,那么它只有4次进入if块而其余2个被忽略。但实际上它应该进入if语句6次。这就是我的意思当我说案件时。

相关问题