我正在从sql server解析xml字符串。但是我的xml字符串有嵌套标记,我正在共享xml字符串。
<abc sn="ahdahsdaud" family="abc" style="Aa" model="abc111222">
<part number="7N8901160101" fnum="5" qty="1.000" uom="EA">
<l2part number="075-76067-000" fnum="9000" qty="0.000" uom="EA" />
</part>
<part number="392-41230-001" fnum="440" qty="1.000" uom="EA">
<l2part number="092-41230-000" fnum="9000" qty="0.000" uom="EA" />
</part>
</abc>
所以我在这里使用以下代码获取部分标记属性:
List<abBOM> list = new ArrayList<abBOM>();//pojo
abBOM abbom = null;
while(rs.next()){
SQLXML plan = rs.getSQLXML("XMLATT");//XMLAttribute
String xmlstr=plan.getString();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new InputSource(new StringReader(xmlstr)));
document.getDocumentElement().normalize();
Element root = document.getDocumentElement();
// System.out.println(root.getNodeName());
NodeList nList = document.getElementsByTagName("part");
// System.out.println("size"+nList.getLength());
for (int temp = 0; temp < nList.getLength(); temp++)
{
Node node = nList.item(temp);
if (node.getNodeType() == Node.ELEMENT_NODE)
{
Element eElement = (Element) node;
//System.out.println(eElement.getElementsByTagName("number").item(0).getTextContent());
abbom = new abBOM();
abbom.setNumber(eElement.getAttribute("number"));
abbom.setfindNumber(eElement.getAttribute("fnum"));
abbom.setQty(eElement.getAttribute("qty"));
abbom.setUom(eElement.getAttribute("uom"));
list.add(abbom);
}
}
}
输出清单:
[[7N8901160101,5,1.000,EA], [392-41230-001,440,1.000,EA]]
我想将l2part标签属性存储在同一个列表中,即。部分属性然后l2part这样的属性。 Plz建议任何方式。
提前致谢。
答案 0 :(得分:0)
在处理零件元素后,将l2part作为新的NodeList读取。
您可以重构代码以递归方式执行此操作。假设您的XML非常简单:
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.StringReader;
public class T {
static String data = "<abc sn=\"ahdahsdaud\" family=\"abc\" style=\"Aa\" model=\"abc111222\">\n" +
"<part number=\"7N8901160101\" fnum=\"5\" qty=\"1.000\" uom=\"EA\">\n" +
"<l2part number=\"075-76067-000\" fnum=\"9000\" qty=\"0.000\" uom=\"EA\" />\n" +
"</part>\n" +
"<part number=\"392-41230-001\" fnum=\"440\" qty=\"1.000\" uom=\"EA\">\n" +
"<l2part number=\"092-41230-000\" fnum=\"9000\" qty=\"0.000\" uom=\"EA\" />\n" +
"</part>\n" +
"</abc>";
public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new InputSource(new StringReader(data)));
document.getDocumentElement().normalize();
Element root = document.getDocumentElement();
// System.out.println(root.getNodeName());
NodeList nList = document.getElementsByTagName("part");
// System.out.println("size"+nList.getLength());
processNodeList(nList);
}
private static void processNodeList(NodeList nList) {
for (int temp = 0; temp < nList.getLength(); temp++)
{
Node node = nList.item(temp);
if (node.getNodeType() == Node.ELEMENT_NODE)
{
Element eElement = (Element) node;
//System.out.println(eElement.getElementsByTagName("number").item(0).getTextContent());
abbom = new abBOM();
abbom.setNumber(eElement.getAttribute("number"));
abbom.setfindNumber(eElement.getAttribute("fnum"));
abbom.setQty(eElement.getAttribute("qty"));
abbom.setUom(eElement.getAttribute("uom"));
list.add(abbom);
NodeList lpart2 = eElement.getElementsByTagName("l2part");
processNodeList(lpart2);
}
}
}
}
请注意,如果您有嵌套级别的l2part,它们也会被处理。否则它将读取一个空列表。可能需要根据您的数据进行细化
答案 1 :(得分:0)
只需在内部重复循环,就像这样:
NodeList nList = document.getElementsByTagName("part");
// System.out.println("size"+nList.getLength());
for (int temp = 0; temp < nList.getLength(); temp++)
{
Element eElement = (Element) nList.item(temp);
// System.out.println(eElement.getElementsByTagName("number").item(0).getTextContent());
abbom = new abBOM();
abbom.setNumber(eElement.getAttribute("number"));
abbom.setfindNumber(eElement.getAttribute("fnum"));
abbom.setQty(eElement.getAttribute("qty"));
abbom.setUom(eElement.getAttribute("uom"));
list.add(abbom);
NodeList list2 = eElement.getElementsByTagName("l2part");
for (int t2 = 0; t2 < list2.getLength(); t2++)
{
eElement = (Element) list2.item(t2);
abBOM abbom2 = new abBOM();
abbom2.setNumber(eElement.getAttribute("number"));
abbom2.setfindNumber(eElement.getAttribute("fnum"));
abbom2.setQty(eElement.getAttribute("qty"));
abbom2.setUom(eElement.getAttribute("uom"));
list.add(abbom2);
}
}