Java 如何将xml文件的字符串值转换为整数

Java 如何将xml文件的字符串值转换为整数,java,xml,Java,Xml,我有一个java代码,它解析xml文件以获取主题的id,我想将此字符串转换为整数,我使用了BigInteger,但它不起作用: public void topicid(){ File inputFile = new File("D:\\Topic.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); try {

我有一个java代码,它解析xml文件以获取主题的id,我想将此字符串转换为整数,我使用了BigInteger,但它不起作用:

    public void topicid(){
        File inputFile = new File("D:\\Topic.xml");
        DocumentBuilderFactory dbFactory  = DocumentBuilderFactory.newInstance();
        try {   
             DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
             Document doc = dBuilder.parse(inputFile);

             doc.getDocumentElement().normalize();

             System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
             NodeList nList = doc.getElementsByTagName("top");

             for (int temp = 0; temp < nList.getLength(); temp++) {
             Node nNode = nList.item(temp);
             System.out.println("----------------------------------------------------------------------------");
             System.out.println("\n La requete :" + nNode.getNodeName());
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
               Element eElement = (Element) nNode;
           String idtopic=eElement.getElementsByTagName("querytweettime").item(0).getTextContent();
           System.out.println("l'id de la requete est:" + idtopic);
          BigInteger idq= new BigInteger(idtopic);

   System.out.println("l'id final de topic :" +idq);

}
public void topicid(){
File inputFile=新文件(“D:\\Topic.xml”);
DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
试试{
DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
Document doc=dBuilder.parse(inputFile);
doc.getDocumentElement().normalize();
System.out.println(“根元素:+doc.getDocumentElement().getNodeName());
NodeList nList=doc.getElementsByTagName(“顶部”);
对于(int-temp=0;temp
要将
字符串
转换为
整数
,请使用
整数
包装器

Integer.valueOf(my_string);
或者,对于
biginger
,使用
biginger
包装器

BigInteger.valueOf(my_string);

idtopic
字符串可能包含无法转换为整数的无效字符

您能否提供当前代码的输出以获得更详细的答案?

您只需使用

int i = Integer.parseInt(string)
该类具有XML所需的所有转换

用于解析
int

int i = DatatypeConverter.parseInt(string);

对于
long
short
biginger
,…

还有其他方法,所以,您想要一个整数,或者一个biginger?那么“它不起作用”意味着什么?您的输入是什么,预期输出是什么,实际输出是什么?我有一条消息[java.lang.NumberFormatException:对于输入字符串:”“],我的id主题是34952194402811904,例如
eeelement.getElementsByTagName(“querytweettime”).item(0).getTextContent();
返回一个空字符串。尝试打印父节点的内容。我打印内容,但问题是无法将其转换为BigInteger(id=34952194402811904的示例)因此,用户BigInteger包装器代替了Integer包装器:
BigInteger.valueOf(idtopic)
i在使用[int i=DatatypeConverter.parseInt(string);]时有此输出:id 32569981321347074主题的最终id是:-1224736766@cyli对于
int
,您的值太大。您需要使用
long i=DatatypeConverter.parseLong(string);
如果@cyli你确定吗?
long idq=DatatypeConverter.parseLong(“32569981321347074”);结果是相同的“negatif”@cyli;
适合我。