0

Is there a possibility to remove root element of xml file? How can I do this in Java? My xml file looks like this:

<root>
<body>
....
</body>
</root>

I already tried to parse it by Sax and ok I implement this, but I found out that SAX doesn't allow to write code to xml file(I read that is really hard). Have you got any idea how to get content from root tag and write it to new document with body as parent?

PSal
  • 1
  • 1
  • 2
  • SAX Is very low-level. You should use DOM instead. Start by reading about [DocumentBuilderFactory](http://docs.oracle.com/javase/8/docs/api/javax/xml/parsers/DocumentBuilderFactory.html), and follow the links to DocumentBuilder and Document. – VGR Oct 04 '16 at 14:16
  • Possible duplicate of [How to remove the root node of an XML document with DOM](http://stackoverflow.com/questions/4198967/how-to-remove-the-root-node-of-an-xml-document-with-dom) – Pinkie Swirl Oct 04 '16 at 14:58

2 Answers2

0

One way to solve your problem is to parse to document using the Scilca XML Progession package available at GitHub. You can get the structure of the child element then save it.

import org.scilca.sxp.*;

XMLConnection xc = new XMLConnection("@filepath");
List<Node> allBodies = xc.searchMatches("body"); // as body is second tag it will be first in list
Node neededNode = allBodies.get(0);

//Writing Portion

Document data = new Document(neededNode);
XMLWriter xw = data.getWriter();
xw.saveXML("@filepath2");
Shukant Pal
  • 706
  • 6
  • 19
-1

If you only need to strip the <root> and </root> tags you don't need to parse the xml file with SAX etc. Just parse it as String and replace the tags with empty strings.

A very very basic example could be:

  String str = "<root><body></body></root>";

  String str2 = str.replace("<root>", "").replace("</root>", "");
  System.out.println(str2);
pleft
  • 7,567
  • 2
  • 21
  • 45
  • 2
    When I see XML or HTML with regex in one sentence I always refer to [this](http://stackoverflow.com/a/1732454/5091346) answer. – Andrii Abramov Oct 04 '16 at 14:06
  • 2
    @AndriiAbramov Technically, this code is not using a regular expression, but it’s still using blind string replacement, which is a very bad idea for XML and HTML, for the same reason regular expressions are a very bad idea. – VGR Oct 04 '16 at 14:12
  • 1
    Thanks for pointing that my answer is wrong, however since no regex is used, please elaborate why striping the `` and `` tags is wrong since according to my understanding, the OP is not requested to do any fancy stuff with xml parsing. In my answer I emphasize that it is a very basic sample of code probably doing the job the OP wanted. And no need to downvote anymore, just post the correct answer. – pleft Oct 04 '16 at 14:39
  • @elefasGR It's not safety,because may be part of data passing in xml, so it's better to use dom4j to replace root element with body, it's easy, safety and fast way to solve this problem – PSal Oct 05 '16 at 11:40