简体   繁体   中英

How to read hexadecimal value from xml file using java

i am trying to fetch data from xml file using java.

using mysqldump the database table is converted to xml.

table contains one field which is in BLOB type.

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.

Java code that i tried is

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.

Note : I tried to put the xml file in stackoverflow but it does not allow me to add xml content.

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

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. I'm assuming that you you're treating your BLOB as binary content.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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