初学者很多不是太懂,求助一个XML分组函数嵌套太深解析的

5161人阅读
今天代码解析一段XML时报错,经过检查发现里面有嵌套CDATA标志的情况,导致XML结构发生了变化,不能转换为xml对象了,百度下,找到下面的解决方案,测试下好使,分享下!
在CDATA内部的所有内容都会被解析器忽略。一个 CDATA 部件以"&![CDATA[" 标记开始,以"]]&"标记结束。但是要记住,CDATA是不能够嵌套的。但是现在,就是有人要求实现CDATA的嵌套。
比如,将下面这段XML加到另外一个XML的某个元素里面:
&?xml version="1.0" encoding="UTF-8" ?&
&System&&id&library&/id&
&name&Library Management&/name&
&description&&![CDATA[This is a sample library management subsystem for Signet.]]&&/description&
&Categories&
将内部的CDATA的结尾转义?那就变成了"&![CDATA[" 和"]]&",这样不好吧,老师说,接收XML的那一方怎么会知道你在里面加上了转义?
那么,可以把CDATA的结尾的"]]&"破坏掉,把它分到两个CDATA中,然后调用Java的API读取该元素的数据时,实际上API同时读取两个CDATA块,然后合成一个。
那么,把上面的XML作为一个字符串来处理,将"]]&"替换为"]]]]&&![CDATA[",千万要记住,不要包含空格进去。这样,结果应该是
&?xml version="1.0" encoding="UTF-8"?&
&Messages&
&Message type="data"&
&code&5-&/code&
&date&&/date&
&time&10:57:58&/time&
&content&&![CDATA[&?xml version="1.0" encoding="UTF-8" ?&
&System&&id&library&/id&
&name&Library Management&/name&
&description&&![CDATA[This is a sample library management subsystem for Signet.]]]]&&![CDATA[&&/description&
&Categories&
&System/&]]&&/content&
&/Message&
&/Messages&
这样,利用Object org.dom4j.Element.getData()方法得到的数据才是原始的XML片段。
CDATA 在xml中使用比较频繁,当我们在给节点的值中有特殊字符时,就需要把值用CDATA括起来,这样就不解析里面的内容了。但是当遇到里面的值中已有CDATA时就出错了。它是不允许嵌套的。今天在使用soapUI时,发现它展示的xml中成功的使用了CDATA嵌套,自己写了个程序试了下,果然管用。
代码如下。
import java.io.StringR
import org.jdom.*;
import org.jdom.output.*;
import org.jdom.input.*;
@author Administrator
public class CDATATest
Creates a new instance of CDATATest */
public CDATATest()
public static void main(String[]
SAXBuilder
saxb=new SAXBuilder();
doc= null;
StringReader
strreader= null;
attribute=null;
xml="&?xml
version=\"1.0\" encoding=\"utf-8\" ?&" +
"&catalogs&" +
"&catalog1&" +
"&![CDATA[&?xml
version=\"1.0\" encoding=\"UTF-8\"?&&Request&&Data&&![CDATA[x&0]]]]&&&![CDATA[&/Data&&/Request&]]&" +
"&/catalog1&" +
"&/catalogs&";
strreader=new StringReader(xml);
doc=saxb.build(strreader);
element=doc.getRootElement();
System.out.println("catalog1的值:"+element.getChildText("catalog1"));
}catch(Exception
e.printStackTrace();为什么XML这么笨重的数据结构仍在广泛应用? - 知乎440被浏览<strong class="NumberBoard-itemValue" title="6分享邀请回答4128 条评论分享收藏感谢收起314 条评论分享收藏感谢收起2706人阅读
Android(63)
最近工程需要解析多层嵌套XML,没有找到合适的公共解析的,要是一个类自己对应写一个解析方法,岂不累死,于是自己写了个通用的解析类。现在还不是很完善,对子类的解析需要提前指定好子类不能动态解析。
我用的是DefaultHandler来处理XML,里面方法执行过程是
public void startDocument() throws SAXException
第一个执行方法。
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException
该方法是继第一个方法后第二个执行方法。
public void characters(char[] ch, int start, int length)
throws SAXException
这个是继startElement方法后的执行方法。
该方法中的ch把所解析的xml的所有数据都保存进来,且ch初始化为2K数据。 start是一个节点"&"的位置。length就是"&"到下一个"&"的长度。
public void endElement(String uri, String localName, String qName)
throws SAXException
若一个节点,比如&name&michael&/name&,在执行完characters后会执行该方法。
如果节点,比如&names&&name&michael&/name&&/names&在执行names节点时,不会执行到该方法。
public void endDocument() throws SAXException
最后一个执行方法。
解析类如下:
import java.io.IOE
import java.io.StringR
import java.lang.reflect.F
import java.lang.reflect.InvocationTargetE
import java.lang.reflect.M
import java.lang.reflect.M
import java.lang.reflect.ParameterizedT
import java.lang.reflect.T
import java.util.L
import java.util.regex.M
import java.util.regex.P
import javax.xml.parsers.ParserConfigurationE
import javax.xml.parsers.SAXP
import javax.xml.parsers.SAXParserF
import org.xml.sax.A
import org.xml.sax.InputS
import org.xml.sax.SAXE
import org.xml.sax.XMLR
import org.xml.sax.helpers.DefaultH
import com.app.dftr.tzsb.zf.model.WcfC
import com.app.dftr.tzsb.zf.model.WcfC
import com.app.dftr.tzsb.zf.model.WcfR
* 类描述: XML解析类
* @author CHD
* @version
* @Date 日期 :
时间 : 下午3:46:34
public class SAXParseXMLTools&T& extends DefaultHandler {
private String TAG = SAXParseXMLTools.class.getSimpleName();
&span style="white-space:pre"& &/span&private S&span style="white-space:pre"& &/span&// 解析内容
&span style="white-space:pre"& &/span&private String objN&span style="white-space:pre"& &/span&// obj名称
&span style="white-space:pre"& &/span&private O&span style="white-space:pre"& &/span&// obj
&span style="white-space:pre"& &/span&private Class&?&
&span style="white-space:pre"& &/span&private ListSets&T& results =
&span style="white-space:pre"& &/span&private Field[] cla_&span style="white-space:pre"& &/span&// obj的所有域
&span style="white-space:pre"& &/span&private Method[] cla_&span style="white-space:pre"& &/span&// obj的方法
&span style="white-space:pre"& &/span&private String tag_&span style="white-space:pre"& &/span&// 当前标签
&span style="white-space:pre"& &/span&private String super_tag_&span style="white-space:pre"& &/span&// 父级标签
&span style="white-space:pre"& &/span&public ListSets&T& getResults() {
&span style="white-space:pre"&
&/span&try {
&span style="white-space:pre"&
&/span&XMLReader reader =
&span style="white-space:pre"&
&/span&SAXParserFactory factory = SAXParserFactory.newInstance();
&span style="white-space:pre"&
&/span&reader = factory.newSAXParser().getXMLReader();
&span style="white-space:pre"&
&/span&reader.setContentHandler(this);
&span style="white-space:pre"&
&/span&reader.parse(new InputSource(new StringReader(content)));
&span style="white-space:pre"&
&/span&} catch (Exception e) {
&span style="white-space:pre"&
&/span&// TODO: handle exception
&span style="white-space:pre"&
&/span&DebugLog.printError(TAG, "解析出现异常" + e.getMessage());
&span style="white-space:pre"&
&span style="white-space:pre"&
&span style="white-space:pre"& &/span&}
&span style="white-space:pre"& &/span&public SAXParseXMLTools(String result, Class&?& clas, String objname) {
&span style="white-space:pre"&
&/span&results = new ListSets&T&();
&span style="white-space:pre"&
&/span&content =
&span style="white-space:pre"&
&/span&cla =
&span style="white-space:pre"&
&/span&objName =
&span style="white-space:pre"&
&/span&try {
&span style="white-space:pre"&
&/span&obj = cla.newInstance();
&span style="white-space:pre"&
&/span&} catch (InstantiationException e) {
&span style="white-space:pre"&
&/span&// TODO Auto-generated catch block
&span style="white-space:pre"&
&/span&e.printStackTrace();
&span style="white-space:pre"&
&/span&} catch (IllegalAccessException e) {
&span style="white-space:pre"&
&/span&// TODO Auto-generated catch block
&span style="white-space:pre"&
&/span&e.printStackTrace();
&span style="white-space:pre"&
&span style="white-space:pre"&
&/span&cla_fields = obj.getClass().getDeclaredFields();// 根据Class对象获取属性
&span style="white-space:pre"&
&/span&cla_methods = obj.getClass().getDeclaredMethods();// 根据Class对象获取所有方法
&span style="white-space:pre"& &/span&}
&span style="white-space:pre"& &/span&@Override
&span style="white-space:pre"& &/span&public void startDocument() throws SAXException {
&span style="white-space:pre"&
&/span&DebugLog.printInfor(TAG, "开始解析!");
&span style="white-space:pre"&
&/span&super.startDocument();
&span style="white-space:pre"& &/span&}
&span style="white-space:pre"& &/span&@Override
&span style="white-space:pre"& &/span&public void endDocument() throws SAXException {
&span style="white-space:pre"&
&/span&DebugLog.printInfor(TAG, "解析完成!");
&span style="white-space:pre"&
&/span&super.endDocument();
&span style="white-space:pre"& &/span&}
&span style="white-space:pre"& &/span&@Override
&span style="white-space:pre"& &/span&public void startElement(String uri, String localName, String qName,
&span style="white-space:pre"&
&/span&Attributes attributes) throws SAXException {
&span style="white-space:pre"&
&/span&if (qName.equals(objName)) {
&span style="white-space:pre"&
&/span&try {
&span style="white-space:pre"&
&/span&obj = cla.newInstance();
&span style="white-space:pre"&
&/span&} catch (InstantiationException e) {
&span style="white-space:pre"&
&/span&// TODO Auto-generated catch block
&span style="white-space:pre"&
&/span&e.printStackTrace();
&span style="white-space:pre"&
&/span&} catch (IllegalAccessException e) {
&span style="white-space:pre"&
&/span&// TODO Auto-generated catch block
&span style="white-space:pre"&
&/span&e.printStackTrace();
&span style="white-space:pre"&
&span style="white-space:pre"&
&/span&super_tag_name = qN
&span style="white-space:pre"&
&/span&} else {
&span style="white-space:pre"&
&/span&tag_name = qN
&span style="white-space:pre"&
&/span&try {
&span style="white-space:pre"&
&/span&Field field = obj.getClass().getDeclaredField(tag_name);
&span style="white-space:pre"&
&/span&Class fieldClazz = field.getType(); // 得到field的class及类型全路径
&span style="white-space:pre"&
&/span&if (fieldClazz.isPrimitive()) { // 【1】 判断是否为基本类型
&span style="white-space:pre"&
&span style="white-space:pre"&
&/span&if (fieldClazz.getName().startsWith("java.lang")) { // getName()返回field的类型全路径;
&span style="white-space:pre"&
&span style="white-space:pre"&
&/span&if (fieldClazz.isAssignableFrom(List.class)) // 【2】判断是否为队列
&span style="white-space:pre"&
&span style="white-space:pre"&
&/span&Type fc = field.getGenericType(); // 关键的地方,如果是List类型,得到其Generic的类型
&span style="white-space:pre"&
&/span&if (null != fc) {
&span style="white-space:pre"&
&/span&if (fc instanceof ParameterizedType) // 【3】如果是泛型参数的类型
&span style="white-space:pre"&
&span style="white-space:pre"&
&/span&ParameterizedType pt = (ParameterizedType)
&span style="white-space:pre"&
&/span&Class genericClazz = (Class) pt
&span style="white-space:pre"&
&/span&.getActualTypeArguments()[0]; // 【4】得到泛型里的class类型对象。
&span style="white-space:pre"&
&/span&String regex = "&" + tag_name + "&(.*?)&/"
&span style="white-space:pre"&
&/span&+ tag_name + "&";
&span style="white-space:pre"&
&/span&Pattern pattern = Pattern.compile(regex);
&span style="white-space:pre"&
&/span&Matcher matcher = pattern.matcher(content);
&span style="white-space:pre"&
&/span&if(matcher.find()) {
&span style="white-space:pre"&
&/span&String tempStr = matcher.group();
&span style="white-space:pre"&
&/span&results.setNextStr(tempStr); // 队列的字符串
&span style="white-space:pre"&
&/span&content = matcher.replaceFirst("");
&span style="white-space:pre"&
&span style="white-space:pre"&
&/span&if(tag_name.equals("City")) {&span style="white-space:pre"& &/span&// 城市
&span style="white-space:pre"&
&/span&SAXParseXMLTools&WcfCity& appointHanlder = new SAXParseXMLTools&WcfCity&(
&span style="white-space:pre"&
&/span&tempStr, genericClazz,
&span style="white-space:pre"&
&/span&genericClazz.getSimpleName());
&span style="white-space:pre"&
&/span&ListSets&WcfCity& results = appointHanlder
&span style="white-space:pre"&
&/span&.getResults();
&span style="white-space:pre"&
&span style="white-space:pre"&
&/span&try {
&span style="white-space:pre"&
&/span&Class[] args1 = new Class[1];
&span style="white-space:pre"&
&/span&args1[0] = List.
&span style="white-space:pre"&
&/span&Method tempMethod = obj.getClass().getDeclaredMethod("set" + tag_name, args1);
&span style="white-space:pre"&
&/span&tempMethod.invoke(obj, results.getResultSet());
&span style="white-space:pre"&
&/span&} catch (Exception e) {
&span style="white-space:pre"&
&/span&// TODO Auto-generated catch block
&span style="white-space:pre"&
&/span&e.printStackTrace();
&span style="white-space:pre"&
&span style="white-space:pre"&
&/span&} else if(tag_name.equals("County")) { // 县城
&span style="white-space:pre"&
&/span&SAXParseXMLTools&WcfCounty& appointHanlder = new SAXParseXMLTools&WcfCounty&(
&span style="white-space:pre"&
&/span&tempStr, genericClazz,
&span style="white-space:pre"&
&/span&genericClazz.getSimpleName());
&span style="white-space:pre"&
&/span&ListSets&WcfCounty& results = appointHanlder
&span style="white-space:pre"&
&/span&.getResults();
&span style="white-space:pre"&
&span style="white-space:pre"&
&/span&try {
&span style="white-space:pre"&
&/span&Class[] args1 = new Class[1];
&span style="white-space:pre"&
&/span&args1[0] = List.
&span style="white-space:pre"&
&/span&Method tempMethod = obj.getClass().getDeclaredMethod("set" + tag_name, args1);
&span style="white-space:pre"&
&/span&tempMethod.invoke(obj, results.getResultSet());
&span style="white-space:pre"&
&/span&} catch (Exception e) {
&span style="white-space:pre"&
&/span&// TODO Auto-generated catch block
&span style="white-space:pre"&
&/span&e.printStackTrace();
&span style="white-space:pre"&
&span style="white-space:pre"&
&/span&} else if(tag_name.equals("Region")) { // 区域
&span style="white-space:pre"&
&/span&SAXParseXMLTools&WcfRegion& appointHanlder = new SAXParseXMLTools&WcfRegion&(
&span style="white-space:pre"&
&/span&tempStr, genericClazz,
&span style="white-space:pre"&
&/span&genericClazz.getSimpleName());
&span style="white-space:pre"&
&/span&ListSets&WcfRegion& results = appointHanlder
&span style="white-space:pre"&
&/span&.getResults();
&span style="white-space:pre"&
&span style="white-space:pre"&
&/span&try {
&span style="white-space:pre"&
&/span&Class[] args1 = new Class[1];
&span style="white-space:pre"&
&/span&args1[0] = List.
&span style="white-space:pre"&
&/span&Method tempMethod = obj.getClass().getDeclaredMethod("set" + tag_name, args1);
&span style="white-space:pre"&
&/span&tempMethod.invoke(obj, results.getResultSet());
&span style="white-space:pre"&
&/span&} catch (Exception e) {
&span style="white-space:pre"&
&/span&// TODO Auto-generated catch block
&span style="white-space:pre"&
&/span&e.printStackTrace();
&span style="white-space:pre"&
&span style="white-space:pre"&
&/span&} else if(tag_name.equals("SecondDeviceType")) { // 区域
&span style="white-space:pre"&
&/span&SAXParseXMLTools&WcfSecondDeviceType& appointHanlder = new SAXParseXMLTools&WcfSecondDeviceType&(
&span style="white-space:pre"&
&/span&tempStr, genericClazz,
&span style="white-space:pre"&
&/span&genericClazz.getSimpleName());
&span style="white-space:pre"&
&/span&ListSets&WcfSecondDeviceType& results = appointHanlder
&span style="white-space:pre"&
&/span&.getResults();
&span style="white-space:pre"&
&span style="white-space:pre"&
&/span&try {
&span style="white-space:pre"&
&/span&Class[] args1 = new Class[1];
&span style="white-space:pre"&
&/span&args1[0] = List.
&span style="white-space:pre"&
&/span&Method tempMethod = obj.getClass().getDeclaredMethod("set" + tag_name, args1);
&span style="white-space:pre"&
&/span&tempMethod.invoke(obj, results.getResultSet());
&span style="white-space:pre"&
&/span&} catch (Exception e) {
&span style="white-space:pre"&
&/span&// TODO Auto-generated catch block
&span style="white-space:pre"&
&/span&e.printStackTrace();
&span style="white-space:pre"&
&span style="white-space:pre"&
&span style="white-space:pre"&
&span style="white-space:pre"&
&span style="white-space:pre"&
&span style="white-space:pre"&
&span style="white-space:pre"&
&/span&} catch (NoSuchFieldException e) {
&span style="white-space:pre"&
&/span&// TODO Auto-generated catch block
&span style="white-space:pre"&
&/span&e.printStackTrace();
&span style="white-space:pre"&
&span style="white-space:pre"&
&span style="white-space:pre"&
&/span&super.startElement(uri, localName, qName, attributes);
&span style="white-space:pre"& &/span&}
&span style="white-space:pre"& &/span&@Override
&span style="white-space:pre"& &/span&public void endElement(String uri, String localName, String qName)
&span style="white-space:pre"&
&/span&throws SAXException {
&span style="white-space:pre"&
&/span&if (qName.equals(super_tag_name)) {
&span style="white-space:pre"&
&/span&results.getResultSet().add((T) obj);
&span style="white-space:pre"&
&span style="white-space:pre"&
&/span&super.endElement(uri, localName, qName);
&span style="white-space:pre"& &/span&}
&span style="white-space:pre"& &/span&@Override
&span style="white-space:pre"& &/span&public void characters(char[] ch, int start, int length)
&span style="white-space:pre"&
&/span&throws SAXException {
&span style="white-space:pre"&
&/span&String temp = new String(ch, start, length);
&span style="white-space:pre"&
&/span&if (tag_name.equals("_WcfReturnState") && (0 == results.getResult())) {// 解析头部
&span style="white-space:pre"&
&/span&results.setResult(Integer.parseInt(temp));
&span style="white-space:pre"&
&/span&} else if (tag_name.equals("_WcfReturnMessage")) {
&span style="white-space:pre"&
&/span&results.setMessage(temp);
&span style="white-space:pre"&
&span style="white-space:pre"&
&/span&if (null != super_tag_name && super_tag_name.equals(objName)) {// 确保在父类下
&span style="white-space:pre"&
&/span&for (Field cla_f : cla_fields) {
&span style="white-space:pre"&
&/span&if (cla_f.getName().equals(tag_name)) {
&span style="white-space:pre"&
&/span&String value = new String(ch, start, length);
&span style="white-space:pre"&
&/span&for (Method cla_method : cla_methods) {
&span style="white-space:pre"&
&/span&String meth = cla_method.getName();
&span style="white-space:pre"&
&/span&// 匹配set方法
&span style="white-space:pre"&
&/span&if (meth != null
&span style="white-space:pre"&
&/span&&& "set".equals(meth.substring(0, 3))
&span style="white-space:pre"&
&/span&&& Modifier.isPublic(cla_method.getModifiers())
&span style="white-space:pre"&
&/span&&& ("set"
&span style="white-space:pre"&
&/span&+ Character.toUpperCase(cla_f.getName()
&span style="white-space:pre"&
&/span&.charAt(0)) + cla_f.getName()
&span style="white-space:pre"&
&/span&.substring(1)).equals(meth)) {
&span style="white-space:pre"&
&/span&// 调用set方法并赋值
&span style="white-space:pre"&
&/span&try {
&span style="white-space:pre"&
&/span&cla_method.invoke(obj, value);
&span style="white-space:pre"&
&/span&} catch (IllegalArgumentException e) {
&span style="white-space:pre"&
&/span&// TODO Auto-generated catch block
&span style="white-space:pre"&
&/span&e.printStackTrace();
&span style="white-space:pre"&
&/span&} catch (IllegalAccessException e) {
&span style="white-space:pre"&
&/span&// TODO Auto-generated catch block
&span style="white-space:pre"&
&/span&e.printStackTrace();
&span style="white-space:pre"&
&/span&} catch (InvocationTargetException e) {
&span style="white-space:pre"&
&/span&// TODO Auto-generated catch block
&span style="white-space:pre"&
&/span&e.printStackTrace();
&span style="white-space:pre"&
&span style="white-space:pre"&
&span style="white-space:pre"&
&span style="white-space:pre"&
&span style="white-space:pre"&
&span style="white-space:pre"&
&span style="white-space:pre"&
&/span&super.characters(ch, start, length);
&span style="white-space:pre"& &/span&}
注释写的很详细。
前端获取list很简洁,两行就ok,
SAXParseXMLTools&WcfEnterprise& appointHanlder = new SAXParseXMLTools&WcfEnterprise&(result, WcfEnterprise.class, WcfEnterprise.class.getSimpleName());
ListSets&WcfEnterprise& results = appointHanlder.getResults();主题 : 迭代嵌套的XML如何解析?
级别: 精灵王
可可豆: 9127 CB
威望: 9127 点
在线时间: 1398(时)
发自: Web Page
迭代嵌套的XML如何解析?&&&
XML样本如下。&navPoint id=&navpoint-3& playOrder=&3&&&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &navLabel&&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&text&BOOK ONE: 1805&/text&&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &/navLabel&&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &content src=&book-01.xml&/&&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &navPoint id=&navpoint-4& playOrder=&4&&&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&navLabel&&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &text&CHAPTER I&/text&&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&/navLabel&&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&content src=&chapter-001.xml&/&&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &/navPoint&&#160;&#160;&#160;&#160;&#160;&#160;&#160;&/navPoint&&navPoint id=&navpoint-5& playOrder=&5&&&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &navLabel&&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&text&BOOK ONE: 1805&/text&&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &/navLabel&&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &content src=&book-02.xml&/&&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &navPoint id=&navpoint-6& playOrder=&6&&&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&navLabel&&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &text&CHAPTER I&/text&&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&/navLabel&&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&content src=&chapter-001.xml&/&&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &/navPoint&&#160;&#160;&#160;&#160;&#160;&#160;&#160;&/navPoint&大家仔细看红色地方。navPoint,是叠加的,同一个名字,而不是名字不同。。本人目前使用TBXML,好像只有,&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;navPoint = [TBXML nextSiblingNamed:@&navPoint& searchFromElement:navPoint];方法,调用了以后,无法找navPoint的孩子的兄弟。只能从一个navPoint孩子,到他自己的兄弟。 例子中就是无法找到:&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &navPoint id=&navpoint-4& playOrder=&4&&, &navPoint id=&navpoint-6& playOrder=&6&&只能是找到3,然后是5,然后是7,,,就这样。请教下,用过TBXML的说说,没用过这个,如果知道什么方式可以更好的解析,也可以。
级别: 新手上路
可可豆: 263 CB
威望: 153 点
在线时间: 114(时)
发自: Web Page
就用NSXMLParser就可以做吧, 如果层次较深,或者像你这种情况, 可以在解析过程中多次设置delegate. SDK上讲解析的地方提到过这种技巧,你可以去看看
级别: 骑士
可可豆: 2020 CB
威望: 2020 点
在线时间: 782(时)
发自: Web Page
用GData 解析啊,你说的解析不出它的孩子节点是什么意思?为什么不能得到它的孩子节点?
级别: 精灵王
可可豆: 9127 CB
威望: 9127 点
在线时间: 1398(时)
发自: Web Page
引用 引用第1楼chennyshan于 08:26发表的&&:就用NSXMLParser就可以做吧, 如果层次较深,或者像你这种情况, 可以在解析过程中多次设置delegate. SDK上讲解析的地方提到过这种技巧,你可以去看看 我看看。NavPoint自己迭代自己,真不好弄。
级别: 精灵王
可可豆: 9127 CB
威望: 9127 点
在线时间: 1398(时)
发自: Web Page
引用 引用第2楼mac-zeng于 09:02发表的&&:用GData 解析啊,你说的解析不出它的孩子节点是什么意思?为什么不能得到它的孩子节点? navPoint 有叫做navPoint的孩子,也有叫做navPoint的兄弟。找到某一个navPoint之后,本来需要寻找他的兄弟,通常这样。但是我这个需要找他的孩子。因为孩子和兄弟一个名字。
级别: 新手上路
可可豆: 70 CB
威望: 70 点
在线时间: 13(时)
发自: Web Page
新人请教&&&navPoint id=&navpoint-3& playOrder=&3&& 中的 id和 playOrder能解析出来么?如何做?求大虾们指导
关注本帖(如果有新回复会站内信通知您)
发帖、回帖都会得到可观的积分奖励。
按"Ctrl+Enter"直接提交
关注CocoaChina
关注微信 每日推荐
扫一扫 关注CVP公众号
扫一扫 浏览移动版

我要回帖

更多关于 分组函数嵌套太深 的文章

 

随机推荐