使用DOM解析xml,使用http://www.developerfusion.com/code/2064/a-simple-way-to-read-an-xml-file-in-java/中定义的方法
<?xml version="1.0" encoding="UTF-8"?>
<apis xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
<api name="getFormFields" requesttype ="GET">
<url>/fieldtype/</url>
<parameters>
<parameter><![CDATA[fieldtype=textbox&fieldId=8668376496495]]></parameter>
</parameters>
</api>
答案 0 :(得分:1)
我的建议是你使用JAXB标准,使用jaxb的好处是:
你可以在这里找到好的教程:
答案 1 :(得分:0)
有一个link,其中包含一步一步的文档。
这是一个示例XML文件:
<bank>
<customer id="1">
<first>John</first>
<last>Smith</last>
<address>1 Broadway, San Fransico, CA</address>
<balance>3230</balance>
</customer>
<customer id="2">
<first>Bill</first>
<last>Gates</last>
<address>555 Microsoft Way, Redmond, WA</address>
<balance>9876543</balance>
</customer>
</bank>
解析它的代码:
public class BankTest {
final StackContentHandler handler = new StackContentHandler();
final Bank bank = new Bank();
public BankTest() {
handler.setElementHandler("bank", "customer", new CustomerHandler());
handler.connect("customer","first", "customer.firstName");
handler.connect("customer","last", "customer.lastName");
handler.connect("customer","address", "customer.address");
handler.connect("customer","balance", "customer.balance");
try {
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
parser.parse(new File("d:\\mybank.xml"), handler);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public class CustomerHandler extends ElementAdapter {
private Customer customer;
public void startElement(ElementHandler parent, String element, Attributes attributes) throws SAXException {
customer = new Customer();
customer.setId(Integer.parseInt(attributes.getValue("id")));
bank.addCustomer(customer);
}
public Customer getCustomer() {
return customer;
}
}
}
了解更多信息,请使用link
答案 2 :(得分:0)
U可以使用Xstream将XML解析为java对象,它简单易学。 这是Xstream Tutorial