没有根节点

时间:2015-07-22 04:52:19

标签: java xml xml-parsing unmarshalling

我需要解析正在生成的xml文件,它没有root元素。请帮我解析一下xml。以下示例Xml内容。资源标记计数不是固定的,它将是varry。使用Resource类通过解析第二个资源标记来获取错误时进行解组。

@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);

1 个答案:

答案 0 :(得分:0)

您必须添加根。见下面的代码

<div class="about" style="width:100%">
   <div class="logo">Logo</div>
   <div class="headerleftpart"><img src='http://adaptive-images.com/content/images/creepy.jpg'/ style='height:150px;widht:150px;'></div>
</div>