如何将xml文件的String值转换为整数

时间:2022-02-15 06:28:19

i have a java code which parse xml file to get the id of the topic, i want to convert this String into integer, i have used BigInteger but it does not work:

我有一个java代码解析xml文件以获取主题的ID,我想将此String转换为整数,我使用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);

}

4 个解决方案

#1


0  

The javax.xml.bind.DatatypeConverter class has all conversions needed for XML.

javax.xml.bind.DatatypeConverter类具有XML所需的所有转换。

For parsing an int:

对于解析int:

int i = DatatypeConverter.parseInt(string);

There are other methods for long, short, BigInteger, ...

还有其他方法,长,短,BigInteger,...

#2


1  

You can simply use

你可以简单地使用

int i = Integer.parseInt(string)

#3


0  

idtopic string may contain invalid characters which cannot be converted to integer.

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

Can you provide the output of the current code for a more detailed answer?

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

#4


0  

To convert String to Integer, use Integer wrapper.

要将String转换为Integer,请使用Integer包装器。

Integer.valueOf(my_string);

Or, for BigInteger, use BigInteger wrapper

或者,对于BigInteger,请使用BigInteger包装器

BigInteger.valueOf(my_string);

#1


0  

The javax.xml.bind.DatatypeConverter class has all conversions needed for XML.

javax.xml.bind.DatatypeConverter类具有XML所需的所有转换。

For parsing an int:

对于解析int:

int i = DatatypeConverter.parseInt(string);

There are other methods for long, short, BigInteger, ...

还有其他方法,长,短,BigInteger,...

#2


1  

You can simply use

你可以简单地使用

int i = Integer.parseInt(string)

#3


0  

idtopic string may contain invalid characters which cannot be converted to integer.

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

Can you provide the output of the current code for a more detailed answer?

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

#4


0  

To convert String to Integer, use Integer wrapper.

要将String转换为Integer,请使用Integer包装器。

Integer.valueOf(my_string);

Or, for BigInteger, use BigInteger wrapper

或者,对于BigInteger,请使用BigInteger包装器

BigInteger.valueOf(my_string);