加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

哪些方法可用于从Java中的文件返回有效和无效的XML数据?

发布时间:2020-12-15 08:27:14 所属栏目:Java 来源:网络整理
导读:我有以下数据应该是 XML: ?xml version="1.0" encoding="UTF-8"?Product id1/id descriptionA new product/description price123.45/price/ProductProduct id1/id descriptionA new product/description price123.45/price/ProductProductTTTTT id1/id desc
我有以下数据应该是 XML:

<?xml version="1.0" encoding="UTF-8"?>
<Product>
    <id>1</id>
    <description>A new product</description>
    <price>123.45</price>
</Product>

<Product>
    <id>1</id>
    <description>A new product</description>
    <price>123.45</price>
</Product>

<ProductTTTTT>
    <id>1</id>
    <description>A new product</description>
    <price>123.45</price>
</Product>

<Product>
    <id>1</id>
    <description>A new product</description>
    <price>123.45</price>
</ProductAAAAAA>

所以,基本上我有多个根元素(产品)……

关键是我正在尝试将这些数据转换为2个XML文档,1个用于有效节点,1个用于无效节点.

有效节点:

<Product>
   ...
</Product>

节点无效:< ProductTTTTT> …< / Product>和< Product> ……< / ProductAAAAAA>

然后我在想如何使用JAVA(而不是web)实现这一目标.

>如果我没有错,使用XSD验证它将使整个文件无效,因此不是一个选项.
>使用默认的JAXB解析器(unmarshaller)将导致上面的项目,因为它在内部创建了我的实体的XSD.
>只使用XPath(据我所知)将只返回整个文件,我没有找到像GET这样的方法!VALID(这只是为了解释……)
>使用XQuery(可能?)..顺便说一句,如何将XQuery与JAXB一起使用?
> XSL(T)将在XPath上引发同样的事情,因为它使用XPath来选择内容.

那么……我可以使用哪种方法来实现目标? (如果可能的话,请提供链接或代码)

解决方法

如果文件包含带有以“Product”开头的起始和结束标记的行,您可以:

>只要行以< Product或< / Product开头,就使用文件扫描程序将此文档拆分为单个部分
>尝试使用XML API将提取的文本解析为XML.

>如果成功,请将该对象添加到“良好”格式良好的XML文档列表中

>然后执行任何其他架构验证或有效性检查

>如果它抛出一个解析错误,请抓住它,并将该文本片段添加到需要清理或以其他方式处理的“坏”项列表中

一个让你入门的例子:

package com.stackoverflow.questions.52012383;

import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.StringReader;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class FileSplitter {

    public static void parseFile(File file,String elementName) 
      throws ParserConfigurationException,IOException {

        List<Document> good = new ArrayList<>();
        List<String> bad = new ArrayList<>();

        String start-tag = "<" + elementName;
        String end-tag = "</" + elementName;
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder;
        StringBuffer buffer = new StringBuffer();
        String line;
        boolean append = false;

        try (Scanner scanner = new Scanner(file)) {
            while (scanner.hasNextLine()) {
                line = scanner.nextLine();

                if (line.startsWith(startTag)) {
                    append = true; //start accumulating content
                } else if (line.startsWith(endTag)) {
                    append = false;
                    buffer.append(line); 
                    //instead of the line above,you could hard-code the ending tag to compensate for bad data:
                    // buffer.append(endTag + ">");

                    try { // to parse as XML
                        builder = factory.newDocumentBuilder();
                        Document document = builder.parse(new InputSource(new StringReader(buffer.toString())));
                        good.add(document); // parsed successfully,add it to the good list

                        buffer.setLength(0); //reset the buffer to start a new XML doc

                    } catch (SAXException ex) {
                        bad.add(buffer.toString()); // something is wrong,not well-formed XML
                    }
                }

                if (append) { // accumulate content
                    buffer.append(line);
                }
            }
            System.out.println("Good items: " + good.size() + " Bad items: " + bad.size());
            //do stuff with the good/bad results...
        }
    }

    public static void main(String args[]) 
      throws ParserConfigurationException,IOException {
        File file = new File("/tmp/test.xml");
        parseFile(file,"Product");
    }

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读