我是使用Java和SAX解析器进行XML解析的新手.我有一个非常大的XML文件,因为它的大小我被建议使用SAX解析器.我已经完成了部分任务的解析,它按预期工作.现在,XML作业还剩下一个任务:根据用户的请求删除/更新一些节点.
我可以通过名称找到所有标签,更改其data属性等.如果我能够使用SAX执行这些操作,也可以删除.
示例XML描述了某些情况下的一些功能.用户的输入是"case"的名称(case1,case2).
<ruleset>
<rule id="1">
<condition>
<case1>somefunctionality</case1>
<allow>true</allow>
</condition>
</rule>
<rule id="2">
<condition>
<case2>somefunctionality</case2>
<allow>false</allow>
</condition>
</rule>
</ruleset>
Run Code Online (Sandbox Code Playgroud)
如果用户想要删除其中一种情况(例如case1)而不仅仅是case1标记,则rule必须删除完整标记.如果case1要删除,XML将变为:
<ruleset>
<rule id="2">
<condition>
<case2>somefunctionality</case2>
<allow>false</allow>
</condition>
</rule>
</ruleset>
Run Code Online (Sandbox Code Playgroud)
我的问题是,这可以使用SAX完成吗?此时我无法使用DOM或任何其他解析器.只有其他选择更糟糕:字符串搜索.如何使用SaxParser完成?
我有一个XML文件,我需要在其中搜索特定的标签并更新它的值.问题是,使用Sax解析器是"必须".我必须使用Sax Parser"only"找到这些标签,dom stax j4dom dom4j解析器不在考虑范围内.
我可以通过将我的xml文件转换为字符串并使用sax解析器解析它并按StringBuilder对象追加新值来完成此任务吗?会没事吗?或者你会推荐什么?
我有一个这样的xml:
<Message xmlns="uri_of_message">
<VendorId>1234</VendorId>
<SequenceNumber>1</SequenceNumber>
...other important headers...
<Data>
<Functions xmlns="uri_of_functions_subxml">
<Function1 attr="sth">
<Info>Some_Info</Info>
</Function1>
<Function2>
<Info>Some_Info</Info>
</Function2>
...Functions n...
</Functions>
</Data>
</Message>
Run Code Online (Sandbox Code Playgroud)
我需要提取内部xml
<Functions xmlns="uri_of_functions_subxml">
<Function1 attr="sth">
<Info>Some_Info</Info>
</Function1>
<Function2>
<Info>Some_Info</Info>
</Function2>
...Functions n...
</Functions>
Run Code Online (Sandbox Code Playgroud)
我首先尝试使用字符方法获取内部 xml:
public void startElement(String uri, String localName, String tagName, Attributes attributes) throws SAXException {
if (tagName.equalsIgnoreCase("Data")){
buffer = new StringBuffer();}
}
public void characters(char[] ch, int start, int length) throws SAXException {
if (buffer != null) {
buffer.append(new String(ch, start, length).trim());
}
} …Run Code Online (Sandbox Code Playgroud)