0

我从服务器收到一个XML结构如下的字符串:

<item_list>
    <category>
        <item id="1" name="name1" base64="base64String1" />
        <item id="2" name="name2" base64="base64String2" />
        <item id="3" name="name3" base64="base64String3" />
        <item id="4" name="name4" base64="base64String4" />
        ........
    </category>
    <category>
    ........
    </category>
<item_list>

我需要做的是:当我第一次读取字符串时,读取Base64字符串,将它们保存在一个文件中,然后从这个字符串中删除它们。然后将此字符串写入XML文件。所以在那之后,这XML将是这样的:

<item_list>
    <category>
        <item id="1" name="name1" />
        <item id="2" name="name2" />
        <item id="3" name="name3" />
        <item id="4" name="name4" />
        ........
    </category>
    <category>
    ........
    </category>
<item_list>

我使用哪个解析器显示来读取和删除Base64字符串?哪个最适合这个?

如果有人能给我一个例子或教程如何做到这一点,那就太好了。

4

2 回答 2

2

您可以将 XPath 与表达式查询“//*[@base64]”一起使用。

try {
            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
            builderFactory.setNamespaceAware(true);
            DocumentBuilder builder = builderFactory.newDocumentBuilder();
            Document document = builder.parse(new File("your_xml.xml"));

            XPath xpath = XPathFactory.newInstance().newXPath();
            String expression = "//*[@base64]";

            NodeList nodes = (NodeList) xpath.evaluate(expression, document, XPathConstants.NODESET);
            for (int i = 0; i < nodes.getLength(); i++) {
                Element element = (Element) nodes.item(i);
                String base64 = element.getAttribute("base64");
                element.removeAttribute("base64");
            }

            String newXML = getStringFromNode(document);
        }
        catch (Exception e) {

        }

getStringFromNode 方法

public static String getStringFromNode(Node root) {

        StringBuilder result = new StringBuilder();

        if (root.getNodeType() == 3)
            result.append(root.getNodeValue());
        else { 
            if (root.getNodeType() != 9) {
                StringBuffer attrs = new StringBuffer();
                for (int k = 0; k < root.getAttributes().getLength(); ++k) {
                    attrs.append(" ").append(
                            root.getAttributes().item(k).getNodeName()).append(
                            "=\"").append(
                            TextUtils.htmlEncode(root.getAttributes().item(k).getNodeValue()))
                            .append("\" ");
                }
                result.append("<").append(root.getNodeName()).append(" ")
                        .append(attrs).append(">");
            } else {
                result.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            }

            NodeList nodes = root.getChildNodes();
            for (int i = 0, j = nodes.getLength(); i < j; i++) {
                Node node = nodes.item(i);
                result.append(getStringFromNode(node));
            }

            if (root.getNodeType() != 9) {
                result.append("</").append(root.getNodeName()).append(">");
            }
        }
        return result.toString();
    }

已编辑

同样也可以用于解析字符串。请参阅下面的方法,它将字符串解析为 DOM 文档。现在您可以将此文档用作 XPath 的输入。

public static Document ReadDocument(String xml)
    {
        Document doc = null;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {
            dbf.setNamespaceAware(true);
            DocumentBuilder db = dbf.newDocumentBuilder();
            InputSource is = new InputSource();
            StringReader reader = new StringReader(xml);
            is.setCharacterStream(reader);
            doc = db.parse(is); 
        } catch (Exception e) {
            Log.e("Error", "Error while Parsing Document", e);
            return null;
        }
        return doc;
    }
于 2012-12-18T13:40:31.453 回答
0

Vivek 功能的小改进 -> 处理 CDATA + 使用字段常量:

public static String getStringFromNode(Node root) throws IOException 
{
    StringBuilder result = new StringBuilder();

    if (root.getNodeType() == Node.TEXT_NODE)
        result.append(root.getNodeValue());
    else if(root.getNodeType() == Node.CDATA_SECTION_NODE)
        result.append("<![CDATA["+root.getNodeValue()+"]]>");
    else {
        if (root.getNodeType() != Node.DOCUMENT_NODE) {
            StringBuffer attrs = new StringBuffer();

            for (int k = 0; k < root.getAttributes().getLength(); ++k) {
                attrs.append(" ").append(
                        root.getAttributes().item(k).getNodeName()).append(
                        "=\"").append(
                        root.getAttributes().item(k).getNodeValue())
                        .append("\" ");
            }
            result.append("<").append(root.getNodeName()).append(" ")
                    .append(attrs).append(">");
        } else {
            result.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        }

        NodeList nodes = root.getChildNodes();
        for (int i = 0, j = nodes.getLength(); i < j; i++) {
            Node node = nodes.item(i);
            result.append(getStringFromNode(node));
        }

        if (root.getNodeType() != Node.DOCUMENT_NODE) {
            result.append("</").append(root.getNodeName()).append(">");
        }
    }
    return result.toString();
}
于 2013-01-31T15:25:52.150 回答