【问题标题】:java xml unmarshal for not having root nodejava xml unmarshal 没有根节点
【发布时间】:2015-07-22 04:52:18
【问题描述】:

我需要解析一个正在生成且没有根元素的 xml 文件。请帮助我如何解析该xml。下面的示例 Xml 内容。资源标签计数不固定,它将是 varry 。在使用 Resource 类进行解组时,通过解析 2nd 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);

【问题讨论】:

标签: java xml xml-parsing unmarshalling


【解决方案1】:

你必须添加一个根。见下面的代码

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();
        }

    }
}
​

【讨论】:

  • 您好,感谢您的代码。实际上,资源标签不是固定的,它会有所不同。你不能指望一直只有 3 个资源标签。
  • XML 是使用随机资源标签生成的。我已经创建了资源 DOM 类
  • "resource" 是一个字符串,可以替换为任何变量。代码生成一个 List() 对象,其大小在代码中不受限制。
猜你喜欢
  • 1970-01-01
  • 2020-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多