java利用xmlparser解析字符串,哪位大侠有示例的? 网上找不到相关文档... 30
解析xml字符串,像<req><command>login</command><version>1.0</version></req>...
解析xml字符串 , 像 <req><command>login</command><version>1.0</version></req>
展开
3个回答
展开全部
例:
XML文档:
<?xml version="1.0" encoding="UTF-8"?>
<level>
<begin x="80" y="415" />
<end x="720" y="415" />
<walls count="3">
<wall left="195" top="130" right="201" bottom="477" />
<wall left="400" top="6" right="406" bottom="352" />
<wall left="605" top="伏谨130" right="611" bottom="477" />
</walls>
<holes count="5">
<hole x="130" y="106" />
<hole x="270" y="106" />
<hole x="270" y="424" />
<hole x="470" y="328" />
<hole x="730"烂让 y="60"饥厅局 />
</holes>
</level>
解析:
XmlResourceParser xmlParser = this.mActivity.getResources().getXml(R.xml.level001 + idxLevelXml);
int eventType = xmlParser.getEventType();
while (eventType != XmlResourceParser.END_DOCUMENT)
{
if(eventType == XmlResourceParser.START_TAG)
{
String strName = xmlParser.getName();
if (strName.equals("begin"))
{
int m = xmlParser.getAttributeIntValue(0, -1);
int n = xmlParser.getAttributeIntValue(1, -1);
setBBegin(m, n);
}
else if(strName.equals("end"))
{
int i2 = xmlParser.getAttributeIntValue(0, -1);
int i3 = xmlParser.getAttributeIntValue(1, -1);
setEnd(i2, i3);
}
else if(strName.equals("walls"))
{
int nWallCnt = xmlParser.getAttributeIntValue(0, 0);
mwalls = new Rect[nWallCnt];
int nCount = 0;
do
{
if(XmlResourceParser.START_TAG == xmlParser.getEventType()
&& xmlParser.getName().equals("wall"))
{
int iLeft = xmlParser.getAttributeIntValue(0, -1);
int iTop = xmlParser.getAttributeIntValue(1, -1);
int iRight = xmlParser.getAttributeIntValue(2, -1);
int iBottom = xmlParser.getAttributeIntValue(3, -1);
mWalls[nCount] = new Rect(iLeft, iTop, iRight, iBottom);
nCount ++;
}
xmlParser.next();
}while(nCount < nWallCnt);
}
else if (strName.equals("holes"))
{
int nHoleCnt = xmlParser.getAttributeIntValue(0, 0);
mHoles = new Point[nHoleCnt];
int nCount = 0;
do
{
if(XmlResourceParser.START_TAG == xmlParser.getEventType()
&& xmlParser.getName().equals("hole"))
{
int iX = xmlParser.getAttributeIntValue(0, -1);
int iY = xmlParser.getAttributeIntValue(1, -1);
mHoles[nCount] = new Point(iX, iY);
nCount++;
}
xmlParser.next();
}while(nCount < nHoleCnt);
}
}
eventType = xmlParser.next();
}
xmlParser.close();
XML文档:
<?xml version="1.0" encoding="UTF-8"?>
<level>
<begin x="80" y="415" />
<end x="720" y="415" />
<walls count="3">
<wall left="195" top="130" right="201" bottom="477" />
<wall left="400" top="6" right="406" bottom="352" />
<wall left="605" top="伏谨130" right="611" bottom="477" />
</walls>
<holes count="5">
<hole x="130" y="106" />
<hole x="270" y="106" />
<hole x="270" y="424" />
<hole x="470" y="328" />
<hole x="730"烂让 y="60"饥厅局 />
</holes>
</level>
解析:
XmlResourceParser xmlParser = this.mActivity.getResources().getXml(R.xml.level001 + idxLevelXml);
int eventType = xmlParser.getEventType();
while (eventType != XmlResourceParser.END_DOCUMENT)
{
if(eventType == XmlResourceParser.START_TAG)
{
String strName = xmlParser.getName();
if (strName.equals("begin"))
{
int m = xmlParser.getAttributeIntValue(0, -1);
int n = xmlParser.getAttributeIntValue(1, -1);
setBBegin(m, n);
}
else if(strName.equals("end"))
{
int i2 = xmlParser.getAttributeIntValue(0, -1);
int i3 = xmlParser.getAttributeIntValue(1, -1);
setEnd(i2, i3);
}
else if(strName.equals("walls"))
{
int nWallCnt = xmlParser.getAttributeIntValue(0, 0);
mwalls = new Rect[nWallCnt];
int nCount = 0;
do
{
if(XmlResourceParser.START_TAG == xmlParser.getEventType()
&& xmlParser.getName().equals("wall"))
{
int iLeft = xmlParser.getAttributeIntValue(0, -1);
int iTop = xmlParser.getAttributeIntValue(1, -1);
int iRight = xmlParser.getAttributeIntValue(2, -1);
int iBottom = xmlParser.getAttributeIntValue(3, -1);
mWalls[nCount] = new Rect(iLeft, iTop, iRight, iBottom);
nCount ++;
}
xmlParser.next();
}while(nCount < nWallCnt);
}
else if (strName.equals("holes"))
{
int nHoleCnt = xmlParser.getAttributeIntValue(0, 0);
mHoles = new Point[nHoleCnt];
int nCount = 0;
do
{
if(XmlResourceParser.START_TAG == xmlParser.getEventType()
&& xmlParser.getName().equals("hole"))
{
int iX = xmlParser.getAttributeIntValue(0, -1);
int iY = xmlParser.getAttributeIntValue(1, -1);
mHoles[nCount] = new Point(iX, iY);
nCount++;
}
xmlParser.next();
}while(nCount < nHoleCnt);
}
}
eventType = xmlParser.next();
}
xmlParser.close();
追问
嗯~ 谢谢 ... 可以问下 相关jar哪里有下 ? 如果不是文档 是字符串 怎么初始化?
展开全部
org.dom4j.Document document;
try {
document = org.dom4j.DocumentHelper.parseText(strValue);
List projects = document.selectNodes("/datasource/configclob/锋判querycondition/condition"山简);
Iterator it = projects.iterator();
while (it.hasNext()) {
Element elm = (Element) it.next();
if(((Element) elm.selectObject("银唯改columncode")).getText().equalsIgnoreCase(code)){
defaultValue=((Element) elm.selectObject("value")).getText();
break;
}
}
} catch (Exception ee) {
ee.printStackTrace();
}
try {
document = org.dom4j.DocumentHelper.parseText(strValue);
List projects = document.selectNodes("/datasource/configclob/锋判querycondition/condition"山简);
Iterator it = projects.iterator();
while (it.hasNext()) {
Element elm = (Element) it.next();
if(((Element) elm.selectObject("银唯改columncode")).getText().equalsIgnoreCase(code)){
defaultValue=((Element) elm.selectObject("value")).getText();
break;
}
}
} catch (Exception ee) {
ee.printStackTrace();
}
追问
.....dom4j 不要这个
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这个我不懂
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询