0

I need to parse an xml file which is generating and it doesnt have root element. Please help me how to parse that xml. Sample Xml content below. Resource tag count is not fixed and it will be varry . While doing unmarshall using Resource class getting error by parsing 2nd Resource tag.

@XmlRootElement
public class Resource {

    private String name;
    private String checkin;
    private Metrics metrics;
    private Violations violations;

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name
     *            the name to set
     */
    @XmlAttribute(name="Name")
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the checkin
     */
    public String getCheckin() {
        return checkin;
    }

    /**
     * @param checkin
     *            the checkin to set
     */
    @XmlAttribute
    public void setCheckin(String checkin) {
        this.checkin = checkin;
    }

    /**
     * @return the metrics
     */
    public Metrics getMetrics() {
        return metrics;
    }

    /**
     * @param metrics
     *            the metrics to set
     */
    @XmlElement
    public void setMetrics(Metrics metrics) {
        this.metrics = metrics;
    }

    /**
     * @return the violations
     */
    public Violations getViolations() {
        return violations;
    }

    /**
     * @param violations
     *            the violations to set
     */
    @XmlElement
    public void setViolations(Violations violations) {
        this.violations = violations;
    }

<resource Name="src/samp1.js" checkin="true">
  <metrics> 
           <metric Metric_Domain="Size" Name="Lines" Value="8260.0" />
   <metric Metric_Domain="Size" Name="Generated Lines" Value="" /> 
        </metrics>
<resource>
<resource Name="src/samp2.js" checkin="true">
  <metrics> 
           <metric Metric_Domain="Size" Name="Lines" Value="860.0" />
   <metric Metric_Domain="Size" Name="Generated Lines" Value="" /> 
        </metrics>
<resource>
<resource Name="src/samp3.js" checkin="true">
  <metrics> 
           <metric Metric_Domain="Size" Name="Lines" Value="260.0" />
   <metric Metric_Domain="Size" Name="Generated Lines" Value="" /> 
        </metrics>
<resource>
  ----
  ----
  ----
  ----
  goes on
    JAXBContext jaxbContext = JAXBContext.newInstance(Resource.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        return (Resource) jaxbUnmarshaller.unmarshal(xml file path);
chethan
  • 25
  • 7
  • Possible duplicate: http://stackoverflow.com/questions/6640756/parsing-an-xml-stream-with-no-root-element – Flown Jul 22 '15 at 06:16

1 Answers1

0

You have to add a root. See code below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            string input =
                "<resource Name=\"src/samp1.js\" checkin=\"true\">" +
                    "<metrics>" +
                      "<metric Metric_Domain=\"Size\" Name=\"Lines\" Value=\"8260.0\" />" +
                      "<metric Metric_Domain=\"Size\" Name=\"Generated Lines\" Value=\"\" />" +
                    "</metrics>" +
                    "</resource>" +
                  "<resource Name=\"src/samp2.js\" checkin=\"true\">" +
                        "<metrics>" +
                          "<metric Metric_Domain=\"Size\" Name=\"Lines\" Value=\"860.0\" />" +
                          "<metric Metric_Domain=\"Size\" Name=\"Generated Lines\" Value=\"\" />" +
                        "</metrics>" +
                  "</resource>" +
                  "<resource Name=\"src/samp3.js\" checkin=\"true\">" +
                            "<metrics>" +
                              "<metric Metric_Domain=\"Size\" Name=\"Lines\" Value=\"260.0\" />" +
                              "<metric Metric_Domain=\"Size\" Name=\"Generated Lines\" Value=\"\" />" +
                            "</metrics>" +
                  "</resource>";

            input = "<Root>" + input + "</Root>";

            XDocument doc = XDocument.Parse(input);

            var results = doc.Descendants("resource").Select(x => new {
                name = x.Attribute("Name").Value,
                checkin = x.Attribute("checkin").Value,
                metric = x.Descendants("metric").Select(y => new {
                    metric_Domain = y.Attribute("Metric_Domain").Value,
                    name = y.Attribute("Name").Value,
                    value = y.Attribute("Value").Value
                }).ToList()

            }).ToList();
        }

    }
}
​
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • Hi Thanks for your code. Actually that resource tag is not fixed it will vary .you cant expect only 3 resource tag all time. – chethan Jul 22 '15 at 10:57
  • XML is generated with randon resource tag. i have created Resource DOM class – chethan Jul 22 '15 at 11:03
  • "resourse" is a string and can be replaced with any variable. Code produces a List() object that whose size isn't restricted in the code. – jdweng Jul 22 '15 at 11:38