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?

2

2 Answers 2

0

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);
Sign up to request clarification or add additional context in comments.

4 Comments

When I see XML or HTML with regex in one sentence I always refer to this answer.
@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.
Thanks for pointing that my answer is wrong, however since no regex is used, please elaborate why striping the <root> and </root> 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.
@elefasGR It's not safety,because <root> 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
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");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.