我正在尝试使用Simple-XML-framework.但我得到ValueRequiredException
org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.ElementList(data=false, empty=true, entry=, inline=false, name=, required=true, type=void) on field 'Employees' public java.util.List com.example.xmlparsing.Company.Employees for class com.example.xmlparsing.Company at line 2
Run Code Online (Sandbox Code Playgroud)
我的XML文件包含以下内容:
<?xml version="1.0" encoding="utf-8"?>
<Company>
<Emp>
<Name>Venkat</Name>
<ID>661511</ID>
</Emp>
<Emp>
<Name>Shiv</Name>
<ID>661311</ID>
</Emp>
</Company>
Run Code Online (Sandbox Code Playgroud)
我的注释类如下:
Company.java:
package com.example.xmlparsing;
import java.util.List;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;
@Root
public class Company {
@ElementList
public List<Emp> Employees;
}
Run Code Online (Sandbox Code Playgroud)
Emp.java:
package com.example.xmlparsing;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.Text;
@Element(name="Emp")
public class Emp {
@Element
public String Name;
@Element
public String ID; …Run Code Online (Sandbox Code Playgroud) 我有一个我需要解析的XML(我无法控制XML或其格式):
<?xml version="1.0" encoding="UTF-8"?>
<Siri xmlns="http://www.siri.org.uk/siri" version="1.3">
<ResponseTimestamp>2016-01-27T21:49:18.6841619-07:00</ResponseTimestamp>
<VehicleMonitoringDelivery version="1.3">
<ResponseTimestamp>2016-01-27T21:49:18.6841619-07:00</ResponseTimestamp>
<ValidUntil>2016-01-27T21:49:28.6841619-07:00</ValidUntil>
<VehicleActivity>
<RecordedAtTime>2016-01-27T21:49:18.6841619-07:00</RecordedAtTime>
<MonitoredVehicleJourney>
<LineRef>750</LineRef>
<DirectionRef>SOUTHBOUND</DirectionRef>
<FramedVehicleJourneyRef>
<DataFrameRef>2016-01-27T00:00:00-07:00</DataFrameRef>
<DatedVehicleJourneyRef>2519014</DatedVehicleJourneyRef>
</FramedVehicleJourneyRef>
<PublishedLineName>FRONTRUNNER</PublishedLineName>
<OriginRef>601084</OriginRef>
<DestinationRef>801164</DestinationRef>
<Monitored>True</Monitored>
<VehicleLocation>
<Longitude>-111.86847</Longitude>
<Latitude>40.401028</Latitude>
</VehicleLocation>
<ProgressRate>1</ProgressRate>
<CourseOfJourneyRef>18127</CourseOfJourneyRef>
<VehicleRef>101</VehicleRef>
<MonitoredCall>
<StopPointRef>801160</StopPointRef>
<VisitNumber>1</VisitNumber>
<VehicleAtStop>false</VehicleAtStop>
</MonitoredCall>
<OnwardCalls>
<OnwardCall>
<StopPointRef>23076</StopPointRef>
<VisitNumber>1</VisitNumber>
<StopPointName>OREM CENTRAL STATION</StopPointName>
</OnwardCall>
</OnwardCalls>
<Extensions>
<LastGPSFix>2016-01-27T21:49:09.473</LastGPSFix>
<Scheduled>False</Scheduled>
<Bearing>137.91188191173691</Bearing>
<Speed>76.7898894465909</Speed>
<DestinationName>Provo</DestinationName>
</Extensions>
</MonitoredVehicleJourney>
</VehicleActivity>
</VehicleMonitoringDelivery>
</Siri>
Run Code Online (Sandbox Code Playgroud)
我有一个超级基础和剥离类车辆只有根元素,像这样:
import org.simpleframework.xml.Root;
@Root(name="VehicleActivity")
public class Vehicle
{
public Vehicle(){}
}
Run Code Online (Sandbox Code Playgroud)
在xml中,我只关心VehicleActivity标签内的数据.
当我尝试解析它时,从改造回调的成功方法,我得到错误Attribute 'version' does not have a match in …
提出了赏金,因为唯一的答案并没有为Android提供良好的实现.是否有与Android兼容的更快速的实现?或者SimpleXML是我得到的最佳性能?
我是Java和Android开发的新手,因此不知道将xml字符串反序列化为对象的正确过程.我发现了一种适用于的方法:
public static Object deserializeXMLToObject(String xmlFile,Object objClass) throws Exception
{
try
{
InputStream stream = new ByteArrayInputStream(xmlFile.getBytes("UTF-8"));
Serializer serializer = new Persister();
objClass = serializer.read(objClass, stream);
return objClass;
}
catch (Exception e)
{
return e;
}
}
Run Code Online (Sandbox Code Playgroud)
xmlFile(错误命名的)xml字符串在哪里,并且objClass是我想要反序列化的类的空类.这通常是其他对象的列表.
示例类:
@Root(name="DepartmentList")
public class DepartmentList {
@ElementList(entry="Department", inline=true)
public List<Department> DepartmentList =new ArrayList<Department>();
public boolean FinishedPopulating = false;
}
Run Code Online (Sandbox Code Playgroud)
系类:
public class Department {
@Element(name="DeptID")
private String _DeptID ="";
public String DeptID()
{
return _DeptID;
}
public void …Run Code Online (Sandbox Code Playgroud) 编辑:删除了以前的编辑
我正在尝试反序列化以下内容:
<?xml version="1.0" encoding="UTF-8"?>
<ALL>
<KAMP>
<ID>1</ID>
<SQLTID>1376881200</SQLTID>
<DATO>2013-08-19</DATO>
</KAMP>
...
<KAMP>
<ID>2</ID>
<SQLTID>1376881200</SQLTID>
<DATO>2013-08-19</DATO>
</KAMP>
</ALL>
Run Code Online (Sandbox Code Playgroud)
运用
@Root
public class Matches {
@ElementList
private List<Match> list;
public List getMatches() {
return list;
}
}
Run Code Online (Sandbox Code Playgroud)
和
@Root(name = "KAMP", strict = false)
public class Match{
@Element(name = "ID", required = false)
public String Id;
@Element(name = "SQLTID", required = false)
public String Sqltid;
@Element(name = "DATO", required = false)
public String MatchDate;
}
Run Code Online (Sandbox Code Playgroud)
我一直在
Element 'KAMP' does not …Run Code Online (Sandbox Code Playgroud) 给定以下 XML:
<stuff>
<item id="1"><![CDATA[first stuff...]]></item>
<item id="2"><![CDATA[more stuff...]]></item>
</stuff>
Run Code Online (Sandbox Code Playgroud)
我正在努力弄清楚如何使用简单框架反序列化它。我从以下 Java 类开始:
import java.util.ArrayList;
import java.util.List;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.ElementList;
@Root(name="stuff")
public class Stuff {
@ElementList(inline=true)
public List<Item> itemList = new ArrayList<Item>();
}
Run Code Online (Sandbox Code Playgroud)
和
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;
@Element(name="item", data=true)
public class Item {
@Attribute
public String id;
}
Run Code Online (Sandbox Code Playgroud)
因此,我缺少的部分是如何访问每个项目元素的 CDATA 内容?
我有这个文件XML:
<Name xml:lang="en">English</Name> <Name xml:lang="it">Italian</Name>
Run Code Online (Sandbox Code Playgroud)
而我的班级:
@Element
@Namespace(reference = "en", prefix = "lang")
public class Name {
@Text
private String title;
@Override
public String toString() {
return title.toUpperCase() + "\n";
}
Run Code Online (Sandbox Code Playgroud)
为什么当我做toString时,它不会只打印英文名字?
TNKS
我正在使用Retrofit 2,SimpleXmlConverter我在创建Soap Request对象时面临一个问题,它基本上是一个包含4个元素子元素的元素,每个子元素都是不同的数据类型.
这是XML我想要产生的输出.必须遵守元素顺序:
<prf:container>
<prf:aaa>111111111</prf:aaa>
<prf:bbb>true</prf:bbb>
<prf:element>
<prf:ddd>50</prf:ddd>
<prf:eee>false</prf:eee>
</prf:element>
<prf:ccc>textcontent</prf:ccc>
</prf:container>
Run Code Online (Sandbox Code Playgroud)
现在,这是我的Android类,Container.java代表将被序列化的Soap Request对象:
@Root (name = "prf:container")
@Order(elements={"prf:aaa", "prf:bbb", "prf:element", "prf:ccc"})
public class Container {
@Element (name = "prf:aaa")
private int aaa;
@Element(name = "prf:bbb")
private boolean bbb;
@Element (name = "prf:element", required = false)
private MyElement myElement;
@Element (name = "prf:ccc", required = false)
private String ccc;
}
Run Code Online (Sandbox Code Playgroud)
根据Simple XML框架文档:
默认情况下,字段的序列化按声明顺序完成.
但是,在Android中,至少在某些情况下,情况并非如此.无论我如何在我的Container类中设置字段声明顺序,输出始终具有相同的元素顺序.这是一个已知的错误,正如其他SO帖子中所报道的那样.
尽管如此,这个问题还是有一个解决方案.该Order注解.阅读Javadoc中的更多内容.
我的问题是Order …
我想解析以下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>
Run Code Online (Sandbox Code Playgroud)
我尝试通过以下课程实现这一目标:
@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;
}
Run Code Online (Sandbox Code Playgroud)
但是我得到以下异常:
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
Run Code Online (Sandbox Code Playgroud)
有人知道如何解析吗?
我正在尝试使用简单 XML解析 BART 车站列表,如下所示:https: //api.bart.gov/docs/stn/stns.aspx (链接:http: //simple.sourceforge.net/download /stream/doc/tutorial/tutorial.php)。我已经这样设置了我的反序列化对象:
StationList 对象:
@Root(name = "root")
public class StationList {
@ElementList(name = "stations", inline = true)
private List<Station> stations;
@Element(name = "message", required = false)
private String message;
public StationList() {
}
public List<Station> getStations() {
return stations;
}
public String getMessage() {
return message;
}
}
Run Code Online (Sandbox Code Playgroud)
建站对象:
@Root(name = "station", strict = false)
public class Station {
@Element(name ="name")
private String name;
@Element(name = "abbr")
private String abbr;
@Element(name = "gtfs_latitude") …Run Code Online (Sandbox Code Playgroud) 我必须解析一个可以有两种类型的 XML 文件:
<property>
<value>Some text</value>
</property>
Run Code Online (Sandbox Code Playgroud)
和
<property>
<value>
<item id="first_id"/>
<item id="second_id"/>
<item id="third_id"/>
</value>
</property>
Run Code Online (Sandbox Code Playgroud)
我怎样才能用Java做到这一点?
我创建了一个类:
@Root(strict = false)
public class PropertyValue {
@ElementList(inline = true, required = false)
private List<ItemData> items;
@Text(required = false)
private String text;
}
Run Code Online (Sandbox Code Playgroud)
ItemData是item阶级。
但这是行不通的。该代码给了我一个例外:
org.simpleframework.xml.core.TextException: Text annotation @org.simpleframework.xml.Text(data=false, empty=, required=false) on field 'text' private java.lang.String PropertyValue.text used with elements in class PropertyValue
Run Code Online (Sandbox Code Playgroud) simple-framework ×10
java ×8
xml ×8
android ×5
xml-parsing ×2
parsing ×1
performance ×1
retrofit ×1
retrofit2 ×1
soap ×1