简体   繁体   English

如何使用Java从xml文件读取十六进制值

[英]How to read hexadecimal value from xml file using java

i am trying to fetch data from xml file using java. 我试图使用Java从xml文件中获取数据。

using mysqldump the database table is converted to xml. 使用mysqldump将数据库表转换为xml。

table contains one field which is in BLOB type. 表包含一个BLOB类型的字段。

Table structure : 表结构:

CREATE TABLE `test` (
  `image` BLOB
) ENGINE=INNODB DEFAULT CHARSET=utf8

Back up data with hexadecimal value using following procedure 使用以下过程备份具有十六进制值的数据

mysqldump -uuser -ppassword test test --compact --no-create-info --hex-blob > check.sql --xml

in xml blob field content is in hexadecimal values. xml blob字段中的内容为十六进制值。

Java code that i tried is 我尝试过的Java代码是

public static void main(String args[]) {
    try {

        File fXmlFile = new File("E:\\check.sql");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);
        doc.getDocumentElement().normalize();

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

        for (int temp = 0; temp < nList.getLength(); temp++) {

            Node nNode = nList.item(temp);
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                Element eElement = (Element) nNode;

                System.out.println(" Image " + getTagValueUsingAttributeName("field","image", eElement));
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}


private static String getTagValueUsingAttributeName(String sTag, String attributeName, Element eElement){
    String value="";
    for(int i=0;i<eElement.getElementsByTagName(sTag).getLength();i++){            
        if((eElement.getElementsByTagName(sTag).item(i).getAttributes().getNamedItem("name").getTextContent()).equalsIgnoreCase(attributeName)){
            System.out.println(" - "+eElement.getElementsByTagName(sTag).item(i).getAttributes().getNamedItem("name").getTextContent());
            NodeList nlList = eElement.getElementsByTagName(sTag).item(i).getChildNodes();
            System.out.println(1);
            Node nValue = (Node) nlList.item(0);
            if(nValue!=null)
                System.out.println(2+" - "+(nValue.getNodeType() == Node.TEXT_NODE));
            value = (nValue==null)?" ":nValue.getTextContent();
            break;
        }
    }
    return value;
}

But i can not read the xml file for parsing. 但是我无法读取XML文件进行解析。

Note : I tried to put the xml file in stackoverflow but it does not allow me to add xml content. 注意:我试图将xml文件放入stackoverflow,但是它不允许我添加xml内容。

please help me. 请帮我。

If you need to decode long value encoded as Hex you could use this class: http://commons.apache.org/codec/apidocs/org/apache/commons/codec/binary/Hex.html 如果您需要解码编码为十六进制的长值,则可以使用此类: http : //commons.apache.org/codec/apidocs/org/apache/commons/codec/binary/Hex.html

For example, if you have your value stored in char[] array you may use Hex.decodeHex(char[] data) to convert it into raw byte[] array. 例如,如果您将值存储在char []数组中,则可以使用Hex.decodeHex(char[] data)将其转换为原始byte []数组。 I'm assuming that you you're treating your BLOB as binary content. 我假设您将BLOB视为二进制内容。

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

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