内联列表中具有属性的 SimpleXML 单深度元素
SimpleXML Single Depth Element with Attribute in Inline List
我要解析以下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>
我尝试通过以下方式实现此目的 类:
@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;
}
但是我得到以下异常:
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
有人知道如何解析吗?
提供的代码中没有 OddsValueXMLObject
class 的声明。
假设声明如我所想,
错误消息基本上意味着它需要 amount
元素内的 amount
元素,
像这样:
<amount>
<amount></amount>
</amount>
当然,在您拥有的 XML 中没有这样的东西。
你可以用这个来解析它:
@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;
}
请找到以下工作代码
<?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) 我已经评论了 amount 因为它的相同属性所以抛出错误,你需要修复你的样本 xml.
2) 您的 class 中缺少获胜者和描述。
3) 你需要再定义一个 class 来获取值。
4) 您的根元素丢失**
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;
}
其他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;
}
测试示例
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);
}
}
我正在使用 "simple-xml-2.6.jar"
结果:
由于您在 xml 中声明了两次数量,出现错误“元素 'amount' 在第 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)
有几个 amount
个元素,表示有一个列表。此解决方案使用 javax.xml classes 而不是第三方 SimpleXml 库。 entry
class:
@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;
}
它包含一个值 class,但您可以根据需要将其更改为 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;
}
这包含 amount
class,金额 List
。
@XmlRootElement(name="amount")
public class OddsAmountXMLObject {
@XmlValue
private String iAmount;
@XmlAttribute(name="stake")
private String iStake;
}
您可以使用以下代码进行测试:
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);
输出与输入相同的 xml。这些 classes 还将使用 schemagen
进行往返以创建 .xsd
文件并使用 xjc
将 .xsd
模式编译回 Java POJO .往返之后,此字段名称将更改,因为注释与原始名称不匹配,因此建议您更改字段 (属性) 名称以匹配注释。
我要解析以下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>
我尝试通过以下方式实现此目的 类:
@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;
}
但是我得到以下异常:
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
有人知道如何解析吗?
提供的代码中没有 OddsValueXMLObject
class 的声明。
假设声明如我所想,
错误消息基本上意味着它需要 amount
元素内的 amount
元素,
像这样:
<amount>
<amount></amount>
</amount>
当然,在您拥有的 XML 中没有这样的东西。 你可以用这个来解析它:
@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;
}
请找到以下工作代码
<?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) 我已经评论了 amount 因为它的相同属性所以抛出错误,你需要修复你的样本 xml.
2) 您的 class 中缺少获胜者和描述。
3) 你需要再定义一个 class 来获取值。
4) 您的根元素丢失**
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;
}
其他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;
}
测试示例
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);
}
}
我正在使用 "simple-xml-2.6.jar"
结果:
由于您在 xml 中声明了两次数量,出现错误“元素 'amount' 在第 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)
有几个 amount
个元素,表示有一个列表。此解决方案使用 javax.xml classes 而不是第三方 SimpleXml 库。 entry
class:
@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;
}
它包含一个值 class,但您可以根据需要将其更改为 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;
}
这包含 amount
class,金额 List
。
@XmlRootElement(name="amount")
public class OddsAmountXMLObject {
@XmlValue
private String iAmount;
@XmlAttribute(name="stake")
private String iStake;
}
您可以使用以下代码进行测试:
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);
输出与输入相同的 xml。这些 classes 还将使用 schemagen
进行往返以创建 .xsd
文件并使用 xjc
将 .xsd
模式编译回 Java POJO .往返之后,此字段名称将更改,因为注释与原始名称不匹配,因此建议您更改字段 (属性) 名称以匹配注释。