Java 摘要:提取节点名称
是否可以使用apache digester提取节点名 因此,如果xml看起来像Java 摘要:提取节点名称,java,xml,apache,Java,Xml,Apache,是否可以使用apache digester提取节点名 因此,如果xml看起来像 <furniture> <sofa> ..... </sofa> <coffeeTable> ..... </coffeeTable> </furniture> ..... ..... 是否可以提取节点名“沙发”、“咖啡桌” 我知道使用xpath是可能的,但是使
<furniture>
<sofa>
.....
</sofa>
<coffeeTable>
.....
</coffeeTable>
</furniture>
.....
.....
是否可以提取节点名“沙发”、“咖啡桌”
我知道使用xpath是可能的,但是使用digester是可能的吗
干杯(原始答案)
使用一个简单的规则
为模式“furniture/*”
创建一个摘要
,该规则将第二个参数带到对begin方法的每次调用中,并将其粘贴到您选择的集合中(一个获取所有这些参数的列表,一个仅获取所有唯一名称的集合)
(编辑)
别提了,这有点复杂
这项工作:
public class App
{
final static Rule printRule = new Rule() {
public void begin(String namespace, String name,
Attributes attributes) throws Exception {
System.out.println(name);
}
};
public static void main( String[] args ) throws IOException, SAXException
{
InputStream instr = App.class.getResourceAsStream("/sample.xml");
Digester dig = new Digester();
dig.setRules(new RulesBase(){
public List<Rule> match(String namespaceURI, String pattern) {
return Arrays.asList(printRule);
}
});
dig.parse(instr);
}
}
公共类应用程序
{
最终静态规则printRule=新规则(){
public void begin(字符串名称空间、字符串名称、,
属性)引发异常{
System.out.println(名称);
}
};
公共静态void main(字符串[]args)引发IOException、SAXException
{
InputStream instr=App.class.getResourceAsStream(“/sample.xml”);
消化池挖掘=新消化池();
dig.setRules(新规则数据库(){
公共列表匹配(字符串名称空间URI、字符串模式){
返回数组.asList(printRule);
}
});
挖掘解析(instr);
}
}
此特定示例将打印所有元素名称,包括根furniture
元素。我将让您根据需要调整match()
方法。(原始答案)
使用一个简单的规则
为模式“furniture/*”
创建一个摘要
,该规则将第二个参数带到对begin方法的每次调用中,并将其粘贴到您选择的集合中(一个获取所有这些参数的列表,一个仅获取所有唯一名称的集合)
(编辑)
别提了,这有点复杂
这项工作:
public class App
{
final static Rule printRule = new Rule() {
public void begin(String namespace, String name,
Attributes attributes) throws Exception {
System.out.println(name);
}
};
public static void main( String[] args ) throws IOException, SAXException
{
InputStream instr = App.class.getResourceAsStream("/sample.xml");
Digester dig = new Digester();
dig.setRules(new RulesBase(){
public List<Rule> match(String namespaceURI, String pattern) {
return Arrays.asList(printRule);
}
});
dig.parse(instr);
}
}
公共类应用程序
{
最终静态规则printRule=新规则(){
public void begin(字符串名称空间、字符串名称、,
属性)引发异常{
System.out.println(名称);
}
};
公共静态void main(字符串[]args)引发IOException、SAXException
{
InputStream instr=App.class.getResourceAsStream(“/sample.xml”);
消化池挖掘=新消化池();
dig.setRules(新规则数据库(){
公共列表匹配(字符串名称空间URI、字符串模式){
返回数组.asList(printRule);
}
});
挖掘解析(instr);
}
}
此特定示例将打印所有元素名称,包括根
furniture
元素。我将让您根据需要调整match()
方法。这是ExtendedBaseRules
提供的匹配类型
假设这是furniture.xml
的内容:
<furniture>
<sofa>
<blah/>
</sofa>
<coffeeTable>
<bleh/>
</coffeeTable>
</furniture>
API链接
ExtendedBaseRules
提供的匹配类型
假设这是furniture.xml
的内容:
<furniture>
<sofa>
<blah/>
</sofa>
<coffeeTable>
<bleh/>
</coffeeTable>
</furniture>
API链接