sam*_*ell 1 java xml sax saxparser
XML格式
<em:Employees xmlns:em="http://www.example.com">
<em:Employee id="1">
<age>29</age>
<name>Pankaj</name>
<gender>Male</gender>
<role>Java Developer</role>
<childrens>
<child>
<age>2</age>
<name>Guptha</name>
<gender>Male</gender>
</child>
</childrens>
</em:Employee>
</em:Employees>
Run Code Online (Sandbox Code Playgroud)
爪哇
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
public class XMLParserSAX {
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
SAXParser saxParser = saxParserFactory.newSAXParser();
MyHandler handler = new MyHandler();
saxParser.parse(new File("src/Resources/employees.xml"), handler);
//Get Employees list
List<Employee> empList = handler.getEmpList();
//print employee information
for (Employee emp : empList)
System.out.println(emp);
}
}
Run Code Online (Sandbox Code Playgroud)
在我的DefaultHandler中,当我实现时startElement(),它localName为空,并且qName包含名称为ex的名称空间。em:Employees。
如果我没记错的话,localName应该给我取回没有名称空间的名称。难道我做错了什么?
尝试使SAXParserFactory名称空间知道:
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
saxParserFactory.setNamespaceAware(true);
SAXParser saxParser = saxParserFactory.newSAXParser();
Run Code Online (Sandbox Code Playgroud)
在此处阅读startElement()的详细信息。localName除非解析器知道名称空间,否则您将看到“未定义”。
| 归档时间: |
|
| 查看次数: |
352 次 |
| 最近记录: |