第一个xml文件test_balance1.xml
第二个xml文件test_balance2.xml
如何使用SAX解析器解析两个xml文件并比较java中的特定元素?
Ex:我需要解析
line 12: <brm:CURRENT_BAL>-30</brm:CURRENT_BAL>
line 24: <brm:CURRENT_BAL>0</brm:CURRENT_BAL> of test_balance1.xml and
line 12: <brm:CURRENT_BAL>55</brm:CURRENT_BAL>
line 24: <brm:CURRENT_BAL>20</brm:CURRENT_BAL> of test_balance2.xml
解析后如何比较特定元素,即CURRENT_BAL = -30和CURRENT_BAL = 55,CURRENT_BAL = 0和CURRENT_BAL = 20
答案 0 :(得分:0)
您可以在此处使用XMLUtils +参考答案:Best way to compare 2 XML documents in Java
import oracle.xml.diff.XmlUtils;
import oracle.xml.diff.Options;
import java.io.File;
import org.w3c.dom.Node;
import org.w3c.dom.Document;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
public class textDiff
{
public static void main(String[] args) throws Exception
{
XmlUtils xmlUtils = new XmlUtils();
//Parse the two input files
DocumentBuilderFactory dbFactory =
DocumentBuilderFactory.newInstance();
dbFactory.setNamespaceAware(true);
DocumentBuilder docBuilder =
dbFactory.newDocumentBuilder();
Node doc = docBuilder.parse(new File(args[0]));
Node doc1 = docBuilder.parse(new File(args[1]));
//Run the diff
try
{
Document diffAsDom = xmlUtils.diffToDoc(doc,
doc1, new Options());
}
catch (Exception e)
{
e.printStackTrace();
}
}
}