用于Java的DOM解析器打印相同的值

时间:2013-04-04 13:12:43

标签: java xml dom

我有这个xml文件

<Cdtr>
    <Nm>DEF Electronics</Nm>
    <PstlAdr>
        <AdrLine>Corn Exchange 5th Floor</AdrLine>
        <AdrLine>Mark Lane 55</AdrLine>
        <AdrLine>EC3R7NE London</AdrLine>
        <AdrLine>GB</AdrLine>
    </PstlAdr>
</Cdtr>

我正在尝试使用Java中的dom解析器解析xml,获取叶标记名称(具有值)及其各自的值。以下是我正在使用的代码。

public class GetNodeValues {

  public static void main(String[] args) {

    try {

   String xmlFile = "C:/Users/Administrator/workspace/sample.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("*");
   System.out.println("XML Elements: ");


   for (int i=0; i<list.getLength(); i++) {
     // Get element
    Element element = (Element)list.item(i);
    String nodnam = element.getNodeName();
    NodeList nl = doc.getElementsByTagName(nodnam);
    Element ele = (Element) nl.item(0);
    if (ele.getChildNodes().getLength() > 0) // then it has text

          String val = ele.getChildNodes().item(0).getNodeValue();
          if (val.startsWith("\n")) {         //Discarding pseudo nodes
          }else {
        System.out.println("Node: "+nodnam+" | Val: "+val); //print node names and values
          }
         }
   }
}
else{
   System.out.print("File not found!");
}
}
catch (Exception e) {
  System.exit(1);
   }
  }
}

我得到以下结果。

Node: AdrLine | Val: Corn Exchange 5th Floor
Node: AdrLine | Val: Corn Exchange 5th Floor
Node: AdrLine | Val: Corn Exchange 5th Floor
Node: AdrLine | Val: Corn Exchange 5th Floor

请帮忙。我不明白为什么它重复标记值。预期的输出是

Node: AdrLine | Val: Corn Exchange 5th Floor
Node: AdrLine | Val: Mark Lane 55
Node: AdrLine | Val: EC3R7NE London
Node: AdrLine | Val: GB

2 个答案:

答案 0 :(得分:2)

在解析文档时,使用Document.getElementsByTagName(String tagname)。根据其Javadoc,它会以文档顺序返回文档中的所有Element。由于您的结构有多个AddrLine元素,并且由于您始终从此列表中选择第0个元素,因此它始终为您提供相同的元素。

相反,你可能想写这样的东西:

for (int i = 0; i < list.getLength(); i++) {
    Element element = (Element) list.item(i);
    String nodnam = element.getNodeName();

    if (element.getChildNodes().getLength() > 0) // then it has text
    {
    // etc., etc.

总结一下,当您已经拥有该元素时,不要尝试从该文档中检索该元素。

答案 1 :(得分:0)

这是重新编译的程序,用于打印xml文档中的所有元素

    public static void main(String[] args) {

    try {

        String xmlFile = "staff.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) {
                    String val = element.getChildNodes().item(0)
                            .getNodeValue();
                    if (!val.startsWith("\n"))
                        System.out.println(nodnam + "==" + val);
                }
            }
        } else {
            System.out.print("File not found!");
        }
    } catch (Exception e) {
        System.exit(1);
    }
}

你去吧。它会打印文件中的所有元素。