简体   繁体   English

从Java中的XML文件中删除元素

[英]remove elements from XML file in java

I have generated an xml file from an excel data base and it contains automatically an element called " offset ". 我已经从Excel数据库中生成了一个xml文件,它自动包含一个名为“ offset ”的元素。 To make my new file match my needs, I want to remove this element using java. 为了使新文件符合我的需求,我想使用java删除此元素。 here is the xml content: 这是xml内容:

<Root><models><id>2</id><modelName>Baseline</modelName><domain_id>2</domain_id><description> desctiption </description><years><Y2013>value1</Y2013><Y2014>value2</Y2014><Y2015>value3</Y2015><Y2016>value4</Y2016><Y2017>value5</Y2017></years><offset/></models></Root>

I made a code that reads(with a buffered reader) and writes the content in a new file and used the condition if: 我编写了一个代码来读取(使用缓冲的读取器)并将内容写入新文件,并在以下情况下使用了条件:

        while (fileContent !=null){
        fileContent=xmlReader.readLine();
        if (!(fileContent.equals("<offset/>"))){
            System.out.println("here is the line:"+ fileContent);
            XMLFile+=fileContent;
            }


    }

But it does not work 但这不起作用

I would personally recommend using a proper XML parser like Java DOM to check and delete your nodes, rather than dealing with your XML as raw String s (yuck). 我个人建议使用适当的XML解析器(例如Java DOM)来检查和删除您的节点,而不是将XML作为原始String处理(糟糕)。 Try something like this to remove your 'offset' node. 尝试这样的操作以删除“偏移”节点。

File xmlFile = new File("your_xml.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xmlFile);

NodeList nList = doc.getElementsByTagName("offset");
for (int i = 0; i < nList.getLength(); i++) {
    Node node = nList.item(i);
    node.getParentNode().removeChild(node); 
}

The above code removes any 'offset' nodes in an xml file. 上面的代码删除了xml文件中的所有“偏移”节点。

If resources/speed considerations are an issue (like when your_xml.xml is huge), you would be better off using SAX , which is faster (a little more code intensive) and doesn't store the XML in memory. 如果考虑到资源/速度问题(例如,当your_xml.xml很大时),则最好使用SAX ,它速度更快(代码强度更高)并且不将XML存储在内存中。

Once your Document has been edited you'll probably want to convert it to a String to parse to your OutputStream of choice. 编辑完Document您可能希望将其转换为字符串以解析为所选的OutputStream

Hope this helps. 希望这可以帮助。

Just try to replace the unwanted string with an empty string. 只需尝试用空字符串替换不需要的字符串。 It is quick... and seriously dirty. 它很快...而且很脏。 But might solve your problem quickly... just to reappear later on ;-) 但是可能会很快解决您的问题...只是稍后再出现;-)

fileContent.replace(" <offset/> , ""); fileContent.replace(“ <offset/> ,”“);

   String sXML= "<Root><models><id>2</id><modelName>Baseline</modelName><domain_id>2</domain_id><description> desctiption </description><years><Y2013>value1</Y2013><Y2014>value2</Y2014><Y2015>value3</Y2015><Y2016>value4</Y2016><Y2017>value5</Y2017></years><offset/></models></Root>";
   System.out.println(sXML);
   String sNewXML = sXML.replace("<offset/>", "");
   System.out.println(sNewXML);
String xml = "<Root><models><id>2</id><modelName>Baseline</modelName><domain_id>2</domain_id><description> desctiption </description><years><Y2013>value1</Y2013><Y2014>value2</Y2014><Y2015>value3</Y2015><Y2016>value4</Y2016><Y2017>value5</Y2017></years><offset/></models></Root>";
            xml = xml.replaceAll("<offset/>", "");
            System.out.println(xml);

In your original code that you included you have: 在您包含的原始代码中,您具有:

while (fileContent !=null)

Are you initializing fileContent to some non-null value before that line? 您是否在该行之前将fileContent初始化为一些非null值? If not, the code inside your while block will not run. 如果没有,则while块中的代码将无法运行。

But I do agree with the other posters that a simple replaceAll() would be more concise, and a real XML API is better if you want to do anything more sophisticated. 但是我确实同意其他张贴者的观点,即简单的replaceAll()会更简洁,而如果您想做任何更复杂的事情,那么真正的XML API会更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM