[英]SimpleXML Single Depth Element with Attribute in Inline List
I want to parse the following XML 我想解析以下XML
<entry key="9" type="9">
<value>
<amount stake="10">50000000</amount>
<amount stake="1">5000000</amount>
<winner>0.0</winner>
<description>9 Correct Numbers</description>
</value>
</entry>
I try to achieve this with the follow classes: 我尝试用以下类来实现这一点:
@Root(name="entry")
public class OddsEntryXMLObject {
@Attribute(name="key")
private String iKey;
@Attribute(name="jackpot", required=false)
private String iJackpot;
@Attribute(name="type", required=false)
private String iType;
@Element(name="value")
private OddsValueXMLObject iOddsValueXMLObject;
}
public class OddsAmountXMLObject {
@Element(name="amount")
private String iAmount;
@Attribute(name="stake", required=false)
private String iStake;
}
However I get the following exception: 但是我得到以下异常:
java.lang.RuntimeException: org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.Element(data=false, name=amount, required=true, type=void) on field 'iAmount' private java.lang.String OddsAmountXMLObject.iAmount for class OddsAmountXMLObject at line 1
Anyone know how to parse this? 谁知道如何解析这个?
There is no declaration for OddsValueXMLObject
class in the code provided. 在提供的代码中没有
OddsValueXMLObject
类的声明。 Assuming the declaration is what I think it is, the error message basically means that it expects an amount
element inside an amount
element, like this: 假设声明是我认为的,错误消息基本上意味着它需要一个
amount
元素中的amount
元素,如下所示:
<amount>
<amount></amount>
</amount>
Of course, there is nothing like that in the XML you have. 当然,在你拥有的XML中没有类似的东西。 You can parse it with this instead:
你可以用它解析它:
@Root(name="entry")
public class OddsEntryXMLObject{
@Attribute(name="key")
private String key;
@Attribute(name="jackpot", required=false)
private String jackpot;
@Attribute(name="type", required=false)
private String type;
@Element(name="value")
private OddsValueXMLObject value;
}
@Root(name="value")
public class OddsValueXMLObject{
@ElementList(inline=true)
private java.util.List<OddsAmountXMLObject> amounts;
@Element(name="winner", required=false)
private String winner;
@Element(name="description", required=false)
private String description;
}
@Root(name="amount")
public class OddsAmountXMLObject{
@Attribute(name="stake", required=false)
private String stake;
@Text
private String text;
}
Please find following working code 请查找以下工作代码
<?xml version="1.0" encoding="UTF-8"?>
<entry key="9" type="9">
<value>
<amount stake="10">50000000</amount>
<!- <amount stake="1">5000000</amount> -->
<!-- <winner>0.0</winner>-->
<!--<description>9 Correct Numbers</description> -->
</value>
</entry>
1) I have commented amount since its same attribute so throwing error , you need to fix your sample xml. 1)我已经注释了数量因为它的相同属性所以抛出错误,你需要修复你的样本xml。
2) winner and description is missing in your class. 2)班上缺少获奖者和描述。
3) you need to define one more class to get value . 3)你需要再定义一个类来获得价值。
4) Your Root element is missing** 4)你的Root元素缺失**
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;
@Root(name="amount")
class OddsAmountXMLObject {
@Element(name="amount")
private String iAmount;
@Attribute(name="stake", required=false)
private String iStake;
}
other class 其他课程
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;
@Root(name="entry")
public class OddsEntryXMLObject {
@Attribute(name="key") String iKey;
@Attribute(name="jackpot", required=false)
private String iJackpot;
@Attribute(name="type", required=false)
private String iType;
@Element(name="value")
private OddsAmountXMLObject iOddsValueXMLObject;
}
Test example 测试示例
import java.io.File;
import org.simpleframework.xml.core.Persister;
public class ParseExample {
public static void main(String[] list) throws Exception {
Persister persister = new Persister();
File file = new File("C:\\Users\\290008812\\Jhipster_workspace\\Test\\src\\DOMParserDemo\\NewFile.xml");//My xml path
OddsEntryXMLObject example = persister.read(OddsEntryXMLObject.class, file);
//System.out.println(example.iKey);
//System.out.println(example.amount2);
persister.write(example, System.out);
}
}
I am using "simple-xml-2.6.jar" 我正在使用“simple-xml-2.6.jar”
Result : 结果:
Since you have declared amount two time in xml getting error " Element 'amount' declared twice at line 6" so need to fix your xml structure 因为你已经在xml中声明了两次获取错误“元素'数量'在第6行声明了两次”所以需要修复你的xml结构
Exception in thread "main" org.simpleframework.xml.core.PersistenceException: Element 'amount' declared twice at line 6
at org.simpleframework.xml.core.Variable$Adapter.read(Variable.java:444)
at org.simpleframework.xml.core.Variable$Adapter.read(Variable.java:422)
at org.simpleframework.xml.core.Composite.readVariable(Composite.java:679)
at org.simpleframework.xml.core.Composite.readInstance(Composite.java:627)
There are several amount
elements so that indicates there is a list. 有几个
amount
元素,因此表明有一个列表。 This solution uses the javax.xml classes instead of the third party SimpleXml library. 此解决方案使用javax.xml类而不是第三方SimpleXml库。 The
entry
class: entry
级:
@XmlRootElement(name="entry")
public class OddsEntryXMLObject {
@XmlAttribute(name="key")
private String iKey;
@XmlAttribute(name="jackpot", required=false)
private String iJackpot;
@XmlAttribute(name="type", required=false)
private String iType;
@XmlElement(name="value")
private OddsValueXMLObject value;
}
This holds a value class, though you can change this to a List
if you want: 这包含一个值类,但您可以根据需要将其更改为
List
:
@XmlRootElement(name="value")
public class OddsValueXMLObject {
@XmlElement(name="amount")
List<OddsAmountXMLObject> amounts;
@XmlElement(name="winner")
private String dWinner;
@XmlElement(name="description")
private String sDescription;
}
This holds an amount
class, with a List
of amounts. 这包含
amount
类,带有金额List
。
@XmlRootElement(name="amount")
public class OddsAmountXMLObject {
@XmlValue
private String iAmount;
@XmlAttribute(name="stake")
private String iStake;
}
You can test it with this code: 您可以使用以下代码进行测试:
JAXBContext jc = JAXBContext.newInstance( OddsEntryXMLObject.class, OddsValueXMLObject.class, OddsAmountXMLObject.class );
Unmarshaller u = jc.createUnmarshaller();
JAXBElement<OddsEntryXMLObject> o = u.unmarshal( new StreamSource(getClass().getResourceAsStream("SimpleXML.xml")), OddsEntryXMLObject.class);
Marshaller m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(o, System.out);
Which outputs the same xml as the input. 它输出与输入相同的xml。 These classes will also do a roundtrip using
schemagen
to create a .xsd
file and xjc
to compile the .xsd
schema back to Java POJOs. 这些类还将使用
schemagen
进行往返schemagen
以创建.xsd
文件,并使用xjc
将.xsd
模式编译回Java POJO。 After the roundtrip this field names will change because the annotations don't match original names, hence it is advised that you change the field (property) names to match the annotations. 在往返之后,此字段名称将更改,因为注释与原始名称不匹配,因此建议您更改字段(属性)名称以匹配注释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.