XML 文档 - 检查父节点然后遍历文档
XML Document - Checking Parent Node and then Iterating through the document
我在尝试遍历 XML 文档时遇到了挑战。请帮忙。
- 对于文档 1,我的方法通过首先检查
父节点,然后提取子节点信息。
- 我现在有 2 XML 个输出,第二个与第一个不同,我
检查一个,然后提取必要的信息,但对于
文档二有多个条目我需要提取
- 两个文件彼此不同
- 我正在关注一些在线示例,这很有帮助,请问您是否可以
帮助编写下面文档二的代码
- 调试时我可以检查文档和节点,但我看到了它们
我正在努力提取信息
- int length = nlist.getLength(),这总是Zero/0,改变
getElementsByTagName 它 returns 长度为 3.
此代码适用于第一个文档
NodeList nodeList = xmlDocument.getElementsByTagName("add:MemberKey");
for (int i = 0; i < nodeList.getLength(); i++) {
Node currentNode = nodeList.item(i);
if (currentNode.getNodeType() != Node.ELEMENT_NODE)
continue;
Element currentElement = (Element)currentNode;
String tagValue = null;
tagValue = getElementValue("add:MemberCID",currentElement);
System.out.println(tagValue);
}
这是我的第二份文件
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<bbb:CreateMemberResultAnnotation xmlns:bbb="http://www.google.com">
<ResultHeader>
<abc:Version>1.0</abc:Version>
<abc:ResultNumber>1000</abc:ResultNumber>
<abc:ResultOutcome>Complete.</abc:ResultOutcome>
<abc:ResultReason>New Result</abc:ResultReason>
</ResultHeader>
<p>
<bbb:ActivationTime>20200111215217</bbb:ActivationTime>
<bbb:MemberInst>
<add:MemberKey>
<add:MemberID>123458687</add:MemberID>
<add:MemberSeq>0987654332456</add:MemberSeq>
</add:MemberKey>
<add:ActivationTime>20200111215217</add:ActivationTime>
<add:DeativationTime>20370101000000</add:DeativationTime>
</bbb:MemberInst>
<bbb:MemberInst>
<add:MemberKey>
<add:MemberID>0987699544</add:MemberID>
<add:MemberSeq>999999999999999</add:MemberSeq>
</add:MemberKey>
<add:ActivationTime>20200111215217</add:ActivationTime>
<add:DeativationTime>20370101000000</add:DeativationTime>
</bbb:MemberInst>
<bbb:MemberInst>
<add:MemberKey>
<add:MemberID>4444444444</add:MemberID>
<add:MemberSeq>4444444444444</add:MemberSeq>
</add:MemberKey>
<add:ActivationTime>20200111215217</add:ActivationTime>
<add:DeativationTime>20370101000000</add:DeativationTime>
</bbb:MemberInst>
</CreateMemberResult>
</bbb:CreateMemberResultAnnotation>
</soapenv:Body>
</soapenv:Envelope>
检查我的第二个文档的代码:我很挣扎
public static checkMessageType(org.w3c.dom.Document xmlDocument) {
Element docEle = xmlDocument.getDocumentElement();
NodeList nlist= docEle.getElementsByTagName("bbb:CreateMemberResultAnnotation");
int length = nlist.getLength(); // I am getting a 0 length here,
List<Element> childElement = new ArrayList<>();
for (int i = 0; i < nlist.getLength(); i++) {
//Node node = nlist.item(i).getOwnerDocument().getDocumentElement();
// if (node instanceof Element) {
// childElement.add((Element) node);
if (nlist.item(i).getNodeType() == Node.ELEMENT_NODE) {
Element el = (Element) nlist.item(i);
if (el.getNodeName().contains("bbb:MemberInst")) {
String XMLMemberID = el.getElementsByTagName("add:MemberID").item(0).getTextContent();
String XMLMemberSeq = el.getElementsByTagName("add:MemberSeq").item(0).getTextContent();
}
}
}
您的 xml 是否已解析?
第 10 行的 p 标签是什么?它没有在任何地方关闭。
在第 36 行也有一个 createMemberResult 结束标记,但在此之前没有打开标记
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<bbb:CreateMemberResultAnnotation xmlns:bbb="http://www.google.com">
<ResultHeader>
<abc:Version>1.0</abc:Version>
<abc:ResultNumber>1000</abc:ResultNumber>
<abc:ResultOutcome>Complete.</abc:ResultOutcome>
<abc:ResultReason>New Result</abc:ResultReason>
</ResultHeader>
<bbb:ActivationTime>20200111215217</bbb:ActivationTime>
<bbb:MemberInst>
<add:MemberKey>
<add:MemberID>123458687</add:MemberID>
<add:MemberSeq>0987654332456</add:MemberSeq>
</add:MemberKey>
<add:ActivationTime>20200111215217</add:ActivationTime>
<add:DeativationTime>20370101000000</add:DeativationTime>
</bbb:MemberInst>
<bbb:MemberInst>
<add:MemberKey>
<add:MemberID>0987699544</add:MemberID>
<add:MemberSeq>999999999999999</add:MemberSeq>
</add:MemberKey>
<add:ActivationTime>20200111215217</add:ActivationTime>
<add:DeativationTime>20370101000000</add:DeativationTime>
</bbb:MemberInst>
<bbb:MemberInst>
<add:MemberKey>
<add:MemberID>4444444444</add:MemberID>
<add:MemberSeq>4444444444444</add:MemberSeq>
</add:MemberKey>
<add:ActivationTime>20200111215217</add:ActivationTime>
<add:DeativationTime>20370101000000</add:DeativationTime>
</bbb:MemberInst>
</bbb:CreateMemberResultAnnotation>
</soapenv:Body>
</soapenv:Envelope>
此处测试运行成功:
@Test
public void test() throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
URL resource = getClass().getResource("/yourxml.xml");
Document xmlDocument = builder.parse(resource.openStream());
Element docEle = xmlDocument.getDocumentElement();
NodeList nlist = docEle.getElementsByTagName("bbb:CreateMemberResultAnnotation");
int length = nlist.getLength(); // I am getting a 0 length here,
List<Element> childElement = new ArrayList<>();
for (int i = 0; i < nlist.getLength(); i++) {
// Node node = nlist.item(i).getOwnerDocument().getDocumentElement();
// if (node instanceof Element) {
// childElement.add((Element) node);
if (nlist.item(i).getNodeType() == Node.ELEMENT_NODE) {
Element el = (Element) nlist.item(i);
if (el.getNodeName().contains("bbb:MemberInst")) {
String XMLMemberID = el.getElementsByTagName("add:MemberID").item(0).getTextContent();
String XMLMemberSeq = el.getElementsByTagName("add:MemberSeq").item(0).getTextContent();
}
}
}
}
我在尝试遍历 XML 文档时遇到了挑战。请帮忙。
- 对于文档 1,我的方法通过首先检查 父节点,然后提取子节点信息。
- 我现在有 2 XML 个输出,第二个与第一个不同,我
检查一个,然后提取必要的信息,但对于 文档二有多个条目我需要提取 - 两个文件彼此不同
- 我正在关注一些在线示例,这很有帮助,请问您是否可以 帮助编写下面文档二的代码
- 调试时我可以检查文档和节点,但我看到了它们 我正在努力提取信息
- int length = nlist.getLength(),这总是Zero/0,改变 getElementsByTagName 它 returns 长度为 3.
此代码适用于第一个文档
NodeList nodeList = xmlDocument.getElementsByTagName("add:MemberKey");
for (int i = 0; i < nodeList.getLength(); i++) {
Node currentNode = nodeList.item(i);
if (currentNode.getNodeType() != Node.ELEMENT_NODE)
continue;
Element currentElement = (Element)currentNode;
String tagValue = null;
tagValue = getElementValue("add:MemberCID",currentElement);
System.out.println(tagValue);
}
这是我的第二份文件
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<bbb:CreateMemberResultAnnotation xmlns:bbb="http://www.google.com">
<ResultHeader>
<abc:Version>1.0</abc:Version>
<abc:ResultNumber>1000</abc:ResultNumber>
<abc:ResultOutcome>Complete.</abc:ResultOutcome>
<abc:ResultReason>New Result</abc:ResultReason>
</ResultHeader>
<p>
<bbb:ActivationTime>20200111215217</bbb:ActivationTime>
<bbb:MemberInst>
<add:MemberKey>
<add:MemberID>123458687</add:MemberID>
<add:MemberSeq>0987654332456</add:MemberSeq>
</add:MemberKey>
<add:ActivationTime>20200111215217</add:ActivationTime>
<add:DeativationTime>20370101000000</add:DeativationTime>
</bbb:MemberInst>
<bbb:MemberInst>
<add:MemberKey>
<add:MemberID>0987699544</add:MemberID>
<add:MemberSeq>999999999999999</add:MemberSeq>
</add:MemberKey>
<add:ActivationTime>20200111215217</add:ActivationTime>
<add:DeativationTime>20370101000000</add:DeativationTime>
</bbb:MemberInst>
<bbb:MemberInst>
<add:MemberKey>
<add:MemberID>4444444444</add:MemberID>
<add:MemberSeq>4444444444444</add:MemberSeq>
</add:MemberKey>
<add:ActivationTime>20200111215217</add:ActivationTime>
<add:DeativationTime>20370101000000</add:DeativationTime>
</bbb:MemberInst>
</CreateMemberResult>
</bbb:CreateMemberResultAnnotation>
</soapenv:Body>
</soapenv:Envelope>
检查我的第二个文档的代码:我很挣扎
public static checkMessageType(org.w3c.dom.Document xmlDocument) {
Element docEle = xmlDocument.getDocumentElement();
NodeList nlist= docEle.getElementsByTagName("bbb:CreateMemberResultAnnotation");
int length = nlist.getLength(); // I am getting a 0 length here,
List<Element> childElement = new ArrayList<>();
for (int i = 0; i < nlist.getLength(); i++) {
//Node node = nlist.item(i).getOwnerDocument().getDocumentElement();
// if (node instanceof Element) {
// childElement.add((Element) node);
if (nlist.item(i).getNodeType() == Node.ELEMENT_NODE) {
Element el = (Element) nlist.item(i);
if (el.getNodeName().contains("bbb:MemberInst")) {
String XMLMemberID = el.getElementsByTagName("add:MemberID").item(0).getTextContent();
String XMLMemberSeq = el.getElementsByTagName("add:MemberSeq").item(0).getTextContent();
}
}
}
您的 xml 是否已解析? 第 10 行的 p 标签是什么?它没有在任何地方关闭。 在第 36 行也有一个 createMemberResult 结束标记,但在此之前没有打开标记
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<bbb:CreateMemberResultAnnotation xmlns:bbb="http://www.google.com">
<ResultHeader>
<abc:Version>1.0</abc:Version>
<abc:ResultNumber>1000</abc:ResultNumber>
<abc:ResultOutcome>Complete.</abc:ResultOutcome>
<abc:ResultReason>New Result</abc:ResultReason>
</ResultHeader>
<bbb:ActivationTime>20200111215217</bbb:ActivationTime>
<bbb:MemberInst>
<add:MemberKey>
<add:MemberID>123458687</add:MemberID>
<add:MemberSeq>0987654332456</add:MemberSeq>
</add:MemberKey>
<add:ActivationTime>20200111215217</add:ActivationTime>
<add:DeativationTime>20370101000000</add:DeativationTime>
</bbb:MemberInst>
<bbb:MemberInst>
<add:MemberKey>
<add:MemberID>0987699544</add:MemberID>
<add:MemberSeq>999999999999999</add:MemberSeq>
</add:MemberKey>
<add:ActivationTime>20200111215217</add:ActivationTime>
<add:DeativationTime>20370101000000</add:DeativationTime>
</bbb:MemberInst>
<bbb:MemberInst>
<add:MemberKey>
<add:MemberID>4444444444</add:MemberID>
<add:MemberSeq>4444444444444</add:MemberSeq>
</add:MemberKey>
<add:ActivationTime>20200111215217</add:ActivationTime>
<add:DeativationTime>20370101000000</add:DeativationTime>
</bbb:MemberInst>
</bbb:CreateMemberResultAnnotation>
</soapenv:Body>
</soapenv:Envelope>
此处测试运行成功:
@Test
public void test() throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
URL resource = getClass().getResource("/yourxml.xml");
Document xmlDocument = builder.parse(resource.openStream());
Element docEle = xmlDocument.getDocumentElement();
NodeList nlist = docEle.getElementsByTagName("bbb:CreateMemberResultAnnotation");
int length = nlist.getLength(); // I am getting a 0 length here,
List<Element> childElement = new ArrayList<>();
for (int i = 0; i < nlist.getLength(); i++) {
// Node node = nlist.item(i).getOwnerDocument().getDocumentElement();
// if (node instanceof Element) {
// childElement.add((Element) node);
if (nlist.item(i).getNodeType() == Node.ELEMENT_NODE) {
Element el = (Element) nlist.item(i);
if (el.getNodeName().contains("bbb:MemberInst")) {
String XMLMemberID = el.getElementsByTagName("add:MemberID").item(0).getTextContent();
String XMLMemberSeq = el.getElementsByTagName("add:MemberSeq").item(0).getTextContent();
}
}
}
}