【发布时间】:2012-01-23 20:01:08
【问题描述】:
所以,我正在尝试使用 nodelist 和 documentbuilder 在我的 Android 应用程序中解析 XML 文件。问题是,Documentbuilder.parse() 总是返回 null 并最终抛出 IOException。我猜这与文件路径错误有关。
public class EquiScoreActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ProtocolFactory.GetProtocolFactory().LoadProtocol("EquiScore/assets/Protocols/LC1_2008.xml");
}
}
public class ProtocolFactory
{
private static ProtocolFactory m_Instance = new ProtocolFactory();
Document dom;
private ProtocolFactory()
{
}
public static ProtocolFactory GetProtocolFactory()
{
return m_Instance;
}
public Protocol LoadProtocol(String filename)
{
Protocol output = null;
List<JudgementGroup> jGroups;
List<GeneralAssesment> gAssesment;
ParseXmlFile(filename);
ParseDocument();
return output;
}
private void ParseXmlFile(String filename)
{
//get the factory
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try
{
//Using factory get an instance of document builder
DocumentBuilder db = dbf.newDocumentBuilder();
//parse using builder to get DOM representation of the XML file
dom = db.parse(filename);
}
catch(ParserConfigurationException pce)
{
pce.printStackTrace();
}
catch(SAXException se)
{
se.printStackTrace();
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
}
基本上,我需要知道如何获取正确的文件路径或其他(也许更好?)方法来解析 XML。一种类型的每个节点可以有不同数量的子节点,这就是为什么我认为这种方法会很好。
IOException 是: “格式错误的 URL 异常” “未找到协议:EquiScore/assets/Protocols/LC1_2008.xml”
我尝试了文件名的各种变体,但似乎还是得到了这个错误。
【问题讨论】:
标签: android xml parsing exception