-4

Can we able to parse single node(having only one tag) xml file using Java

Example:

<Node
    e1 =" 1  "
    e2 = "asd " 
 /> 
<Node2
     e1= "2"
     e2 = " asdf"
 />

by using schema file or without using ? I am able to parse if it contains root tag Any help would be appreciated

  • Yes, you can. [Why is “Is it possible to…” a poorly worded question?](https://softwareengineering.meta.stackexchange.com/q/7273/202153) – Andreas Feb 19 '19 at 16:10
  • Sorry by mistakenly I have added wrong format of xml here is correct format please find below – mahendhra 8 Feb 20 '19 at 07:08
  • 1
    I tried to fix the question format for you to display the XML correctly. However, your XML does not seems to be even a valid one. It is also confusing for what you meant by "single node". Please fix your question to provide valid XML, and more explicit and clear question on the problem you are facing – Adrian Shum Feb 20 '19 at 07:15
  • and, I have changed your `\>` to `>`, not sure if it is what you intended to write, as `<.... \>` is not valid XML syntax – Adrian Shum Feb 20 '19 at 07:20
  • Hi by mistakenly I have added wrong xml format yesterday. Now I have edited with correctly one – mahendhra 8 Feb 20 '19 at 07:49
  • I tried with DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); – mahendhra 8 Feb 20 '19 at 08:08
  • I am able to parse if it contains one root tag – mahendhra 8 Feb 20 '19 at 08:08
  • Possible duplicate of [XML file containing multiple root elements](https://stackoverflow.com/q/30984236/5221149) – Andreas Feb 20 '19 at 18:18

1 Answers1

-1

You need a class that can hold the object you are trying to parse (with the right annotations). It's really recommended generating it, because the annotations aren't always so intuitive to write. Your Node element is your root element, you don't need any other wrapping element. Try Googling for JaxB or other XML parsing libraries.

danovia
  • 39
  • 6