我有这个单行 xml 文件(没有缩进和新行),如下所示
<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:camt.054.001.03"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:camt.054.001.03
camt.054.001.03.xsd">
<BkToCstmrDbtCdtNtfctn><GrpHdr><MsgId>0000000006</MsgId>
<CreDtTm>2013-04-
16T14:38:00</CreDtTm>
</GrpHdr>
</BkToCstmrDbtCdtNtfctn></Document>
我正在使用这个 java DOM 解析器程序来解析和检索值
import java.io.File;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class GetNodeValues {
static String value = null;
static ArrayList alist = null;
/****************** GET XPATH FOR EACH TAG **************************************/
public static String getXPath(Element elemnt) {
String xpath = null;
String curNode = elemnt.getNodeName();
ArrayList<String> al = new ArrayList<String>();
al.add(curNode);
// al.add(parNode);
while (!elemnt.getParentNode().getNodeName().equals("#document")) {
al.add(elemnt.getParentNode().getNodeName());
elemnt = (Element) elemnt.getParentNode();
}
for (int i = al.size() - 1; i >= 0; i--) {
xpath = xpath + "/" + al.get(i);
}
return xpath.replaceAll("null", "");
}
/******************************************************************************************/
/**************************** GET TAG NAMES AND VALUES ***********************/
public static ArrayList getValues() {
try {
alist = new ArrayList();
String xmlFile = "C:/Users/Administrator/Desktop/sample2.xml";
File file = new File(xmlFile);
if (file.exists()) {
// Create a factory
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
// Use the factory to create a builder
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(xmlFile);
doc.getDocumentElement().normalize();
// Get a list of all elements in the document
NodeList list = doc.getElementsByTagName("*");
for (int i = 0; i < list.getLength(); i++) {
// Get element
Element element = (Element) list.item(i);
String nodnam = element.getNodeName();
if (element.getChildNodes().getLength() > 0) // then it has
// text
{
String val = element.getChildNodes().item(0)
.getNodeValue();
if (val.startsWith("\n")) { // Discarding pseudo nodes
} else {
value = nodnam + " > " + val + " > "
+ getXPath(element); // print node names and
// values
System.out.println(value);
alist.add(value);
}
}
}
} else {
System.out.print("File not found!");
}
} catch (Exception e) {
System.exit(1);
}
return alist;
}
/********************************************************************************************/
/************************** MAIN METHOD **********************************************/
public static void main(String[] args) {
System.out.println(getValues());
}
}
而且它不打印任何值。但是,如果我编辑 xml 文件并像这样添加缩进和新行
<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:camt.054.001.03" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:camt.054.001.03 camt.054.001.03.xsd">
<BkToCstmrDbtCdtNtfctn>
<GrpHdr>
<MsgId>0000000006</MsgId>
<CreDtTm>2013-04-16T14:38:00</CreDtTm>
</GrpHdr>
</BkToCstmrDbtCdtNtfctn>
</Document>
然后我得到如下输出
MsgId > 0000000006 > /Document/BkToCstmrDbtCdtNtfctn/GrpHdr/MsgId
CreDtTm > 2013-04-16T14:38:00 > /Document/BkToCstmrDbtCdtNtfctn/GrpHdr/CreDtTm
所以问题是我不能编辑每个 xml 文件。要处理的文件很大。我在 java dom 解析器中遗漏了什么吗?我所需要的是程序应该解析和打印没有缩进和新行的 xml 文件的值....