2015-07-22 64 views
0

我需要解析一個正在生成的xml文件,它沒有根元素。請幫助我如何解析該XML。下面的示例Xml內容。資源標籤計數不固定,它將是varry。在解析第二個資源標籤時,使用Resource類獲取錯誤時進行解組。java xml unmarshal沒有根節點

@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"> 
 
\t \t <metrics> \t 
 
      <metric Metric_Domain="Size" Name="Lines" Value="8260.0" /> 
 
\t \t \t <metric Metric_Domain="Size" Name="Generated Lines" Value="" /> \t 
 
     </metrics> 
 
<resource> 
 
<resource Name="src/samp2.js" checkin="true"> 
 
\t \t <metrics> \t 
 
      <metric Metric_Domain="Size" Name="Lines" Value="860.0" /> 
 
\t \t \t <metric Metric_Domain="Size" Name="Generated Lines" Value="" /> \t 
 
     </metrics> 
 
<resource> 
 
<resource Name="src/samp3.js" checkin="true"> 
 
\t \t <metrics> \t 
 
      <metric Metric_Domain="Size" Name="Lines" Value="260.0" /> 
 
\t \t \t <metric Metric_Domain="Size" Name="Generated Lines" Value="" /> \t 
 
     </metrics> 
 
<resource> 
 
    ---- 
 
    ---- 
 
    ---- 
 
    ---- 
 
    goes on

JAXBContext jaxbContext = JAXBContext.newInstance(Resource.class); 
     Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); 
     return (Resource) jaxbUnmarshaller.unmarshal(xml file path); 
+0

可能的重複代碼:http://stackoverflow.com/questions/6640756/parsing-an-xml-stream-with-no-root-element – Flown

回答

0

您必須添加一個根。見下面

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

    } 
} 
​ 
+0

你好感謝你的代碼。實際上那個資源標籤是不固定的,它會有所不同。你不能只期待3個資源標籤。 – chethan

+0

XML是使用randon資源標籤生成的。我創建了Resource DOM類 – chethan

+0

「resourse」是一個字符串,可以用任何變量替換。代碼會生成一個List()對象,其大小在代碼中不受限制。 – jdweng