我的問題的根源是給定的XML與變量節點名。 XML已被使用且無法更改。JAXB改變元素「名」 @XMLElements註釋
我創建了一個XSD爲XML生成corrosponding類,然後通過的DOMParser解析它。
我使用的DOMParser因爲通過JAXB它不能分析,因爲變量標籤名稱,但生成的類是有幫助的。
現在我只需要在類的變量節點的標識符。我認爲最好只更改@XmlElement Annotations元素「name」,並給它一個值,如「?」來識別變量節點,但我不知道是否以及如何改變它。
我已經創建了一個小插曲文件來改變一些類名,並認爲它可以通過改變,但我沒有找到一個解決方案。
例XML:
<Settings>
<Window>
<Position Top="5" Left="5"/>
</Window>
<OpenData>
<variable>
<TableData>
<variable-column00 POS="1" Visible="true" SortDesc="true"/>
<variable-column01 POS="3" Visible="true" SortDesc="true"/>
<variable-column02 POS="2" Visible="true" SortDesc="true"/>
</TableData>
</variable>
<variable>
<UserSettings>
<variable-series1 Color="blue" Caption="Series blue" Visible="true" />
<variable-series2 Color="yellow" Caption="Series yellow" Visible="true" />
<variable-series3 Color="red" Caption="Series red" Visible="true" />
</UserSettings>
<ChartConfig>configuration-json....</ChartConfig>
</variable>
<variable/>
</OpenData>
</Settings>
變量節點將是: 節點名爲「可變的」,節點用「可變柱」和一個數字(表的數據的子節點)開始和用「可變序列」和一個數量開始(的用戶設置的子節點)的節點
這種情況的XSD是:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:complexType name="Window">
<xs:complexContent>
<xs:extension base="SettingsElement">
<xs:sequence>
<xs:element name="Position" type="Position" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="Position">
<xs:complexContent>
<xs:extension base="SettingsElement">
<xs:attribute name="Top" type="xs:int" />
<xs:attribute name="Left" type="xs:int" />
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="OpenData">
<xs:complexContent>
<xs:extension base="SettingsElement">
<xs:sequence>
<!-- this would be the "variable" Node -->
<xs:element name="View" type="View" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="View">
<xs:complexContent>
<xs:extension base="SettingsElement">
<xs:sequence>
<!-- normaly the order would be random, but because i use the DOMParser i don't need to pass that to JAXB -->
<xs:element name="TableData" type="TableData" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="UserSettings" type="UserSettings" minOccurs="0" maxOccurs="1" />
<xs:element name="ChartConfig" type="xs:string" minOccurs="0" maxOccurs="1" />
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="UserSettings">
<xs:complexContent>
<xs:extension base="SettingsElement">
<xs:sequence>
<!-- the next Variable Node name beginning with "variable-series" and then a number -->
<xs:element name="SeriesDefinition" type="SeriesDefinition" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="SeriesDefinition">
<xs:complexContent>
<xs:extension base="SettingsElement">
<xs:attribute name="Color" type="xs:string" />
<xs:attribute name="Caption" type="xs:string" />
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="TableData">
<xs:complexContent>
<xs:extension base="SettingsElement">
<xs:sequence>
<!-- the next Variable Node name beginning with "variable-column" and then a number -->
<xs:element name="Column" type="Column" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="Column">
<xs:complexContent>
<xs:extension base="SettingsElement">
<xs:attribute name="POS" type="xs:int" />
<xs:attribute name="Visible" type="xs:boolean" />
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:element name="SettingsElement">
</xs:element>
<xs:element name="Settings" >
<xs:complexType>
<xs:sequence>
<xs:element name="Window" type="Window" minOccurs="0" />
<xs:element name="OpenData" type="OpenData" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
後,我生成它,我的C美景一覽姑娘將opendata會是這樣:
import java.io.Serializable;
import java.util.ArrayList;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "OpenData", propOrder = {
"view"
})
public class OpenData extends SettingsElement implements Serializable {
private final static long serialVersionUID = 1L;
@XmlElement(name = "View")
protected java.util.List<View> view;
public java.util.List<View> getview() {
if (view == null) {
view = new ArrayList<View>();
}
return this.view;
}
}
我需要一個標識爲我的分析工作,因此像改變註釋
@XmlElement(name = "View")
到
@XmlElement(name = "?")
所以我知道我正在搜索的標籤名稱不是查看,但可以是任何東西。
感謝您的幫助提前。
歡迎來到StackOverflow!請查看[提問問題指南](https://stackoverflow.com/help/asking),特別是[如何創建最小,完整和可驗證示例](https://stackoverflow.com/help/MCVE) – AesSedai101