简体   繁体   English

使用DOM获取具有相同节点名称的xml文件中的XML节点数

[英]Getting XML number of nodes in a xml file with the same node name using DOM

I need to parse a xml file store the values to database.. please consider the below xml 我需要解析一个xml文件,将值存储到数据库中。请考虑以下xml

<Name NameType="Primary Name">
 <NameValue>
   <FirstName>Hassan</FirstName> 
   <MiddleName>Dahir</MiddleName> 
   <Surname>Aweys</Surname> 
   <OriginalScriptName>حسن ظاهرعويس</OriginalScriptName> 
   <OriginalScriptName>ハッサン・ダヒル・アウェイス</OriginalScriptName> 
   <OriginalScriptName>アウェス、ハッサン・ダヒル</OriginalScriptName> 
   <OriginalScriptName>ウワイス、ハッサン・ターヒル</OriginalScriptName> 
 </NameValue>
</Name>

Using DOM parsing,Parsing the xml and getting the values. 使用DOM解析,解析xml并获取值。

private static HashMap<String, String> nameDetails(Element nameDetails,String id) throws SQLException {
        HashMap<String, String> Map = new HashMap<String, String>();
         Multimap<String, String> multiMap = ArrayListMultimap.create();
        String pid = null;
        Map.put(pid, id);
        if(nameDetails.hasChildNodes()){
            //System.out.println(nameDetails.getChildNodes().getLength());
            for(int i=0;i<nameDetails.getChildNodes().getLength();i++){
                //check "Name" has attributes or not
                if(nameDetails.getChildNodes().item(i).hasAttributes())
                {
                    for(int a=0;a<nameDetails.getChildNodes().item(i).getAttributes().getLength();a++)
                    {
                        //Name Type values or attributes of Name
                        Map.put(nameDetails.getChildNodes().item(i).getAttributes().item(a).getNodeName(),nameDetails.getChildNodes().item(i).getAttributes().item(a).getNodeValue());
                    }
                }
                //check "Name" has child nodes are not
                if(nameDetails.getChildNodes().item(i).hasChildNodes())
                {
                    String d =null;
                    //No of child nodes "Name" Has that is no. of NameValue
                    for (int a=0;a<nameDetails.getChildNodes().item(i).getChildNodes().getLength();a++)
                    {
                        //No.of Childs Nodes NameValue has that is fName,lName,etc,.
                        for(int b=0;b<nameDetails.getChildNodes().item(i).getChildNodes().item(a).getChildNodes().getLength();b++)
                        {       Map.put(nameDetails.getChildNodes().item(i).getChildNodes().item(a).getChildNodes().item(b).getNodeName(),nameDetails.getChildNodes().item(i).getChildNodes().item(a).getChildNodes().item(b).getChildNodes().item(0).getNodeValue());
                            }       StringClass.setValuesToDB(nameDetails.getNodeName(), Map);

                    }
                }
            }
        }
        return Map;
    }

Now issue in this case is we need to store multiple values of 现在这种情况下的问题是我们需要存储多个值

    <OriginalScriptName>حسن ظاهرعويس</OriginalScriptName> 
    <OriginalScriptName>ハッサン・ダヒル・アウェイス</OriginalScriptName> 
    <OriginalScriptName>アウェス、ハッサン・ダヒル</OriginalScriptName> 
    <OriginalScriptName>ウワイス、ハッサン・ターヒル</OriginalScriptName> 

into sigle value separated by comma 转换为以逗号分隔的单值

    حسن ظاهرعويس,ハッサン・ダヒル・アウェイス,アウェス、ハッサン・ダヒル,ウワイス、ハッサン・ターヒル

into the map.so that we can get the map value while inserting into the db. 进入map.so,以便在插入db时可以获取map值。

  Map.get("OriginalScriptName","حسن ظاهرعويس,ハッサン・ダヒル・アウェイス,アウェス、ハッサン・ダヒル,ウワイス、ハッサン・ターヒル")

   i.e., Map.get("OriginalScriptName",value)
   let value = ,ハッサン・ダヒル・アウェイス,アウェス、ハッサン・ダヒル,ウワイス、ハッサン・ターヒル

I have use xpath to create a solution.create a java prog based on your xml . 我已经使用xpath创建了一个解决方案。基于您的xml创建一个Java prog。 if xml change then this prog will not work . 如果xml更改,则此编将无法工作。 Another thing : your xml should start with 另一件事:您的xml应该以

 <?xml version="1.0" encoding="UTF-8"?>

as your xml contain different character set other than english 因为您的xml包含除英语以外的其他字符集

eg : 例如:

<?xml version="1.0" encoding="UTF-8"?>
<Name NameType="Primary Name">
<NameValue>
   <FirstName>Hassan</FirstName> 
   <MiddleName>Dahir</MiddleName> 
   <Surname>Aweys</Surname> 
   <OriginalScriptName>حسن ظاهرعويس</OriginalScriptName> 
   <OriginalScriptName>ハッサン・ダヒル・アウェイス</OriginalScriptName> 
   <OriginalScriptName>アウェス、ハッサン・ダヒル</OriginalScriptName> 
   <OriginalScriptName>ウワイス、ハッサン・ターヒル</OriginalScriptName> 
</NameValue>
</Name>

here is the program : 这是程序:

package com.xxxxx;

import org.w3c.dom.*;

import javax.xml.parsers.*;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import java.io.*;
import java.util.HashMap;
public class ReadXMLFileSax {
      public static void main(String argv[]) {
    try {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(new File("D:\\xxxx.xml"));

    Element root = document.getDocumentElement();

    XPath xPath = XPathFactory.newInstance().newXPath();
    NodeList nodes = (NodeList)xPath.evaluate("/Name[@NameType='Primary Name']/NameValue/OriginalScriptName",
            document.getDocumentElement(), XPathConstants.NODESET);
    String OriginalScriptName = "";
    HashMap<String, String> Map = new HashMap<String, String>();
    for (int i = 0; i < nodes.getLength(); ++i) {
        Element e = (Element) nodes.item(i);
      //  System.out.println(e.getTextContent());
        OriginalScriptName = OriginalScriptName+","+e.getTextContent();
    }
    System.out.println("OriginalScriptName "+OriginalScriptName.substring(1));
    Map.put("OriginalScriptName", OriginalScriptName.substring(1));
    }catch (Exception e) {
    e.printStackTrace();
    }

      }
    }

Program output look like : 程序输出如下:

OriginalScriptName حسن ظاهرعويس,ハッサン・ダヒル・アウェイス,アウェス、ハッサン・ダヒル,ウワイス、ハッサン・ターヒル

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM