どちらの方法はJavaでファイルから有効と無効なXMLデータを返すために使用することができますか?

フェリペC.:

私は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>

だから、基本的に私は複数のルート要素を持っています(product)...

ポイントは、私は2つのXML文書、無効なノードのための有効なノードと他の1に、このデータを変換しようとしているということです。

有効なノード:

<Product>
   ...
</Product>

無効なノード:<ProductTTTTT>...</Product><Product>...</ProductAAAAAA>

それから私は、私は、この使用してJAVA(ないウェブ)を達成することができますどのように考えています。

  • 私は間違っていないよ場合は、XSDでそれを検証すること、そうでないオプションをファイル全体が無効になります。
  • 内部的に、それは私のエンティティのXSDを作成するので、デフォルトJAXBパーサ(アンマーシャラー)を使用すると、上記の項目につながります。
  • ちょうど(私が知っているから)XPathを使用すると、ちょうど私がGETのようなものを取得する方法を見つけることができませんでした、ファイル全体を返します!VALID(それは単に説明することです...)
  • XQueryを使用した(多分?)..方法によって、どのようにJAXBにXQueryを使用するには?
  • それは、コンテンツを選択するために、XPathを使用しているため、XSL(T)は、XPathの上の同じことにつながります。

だから... ...どの方法私は目的を達成するために使用することができますか?(そして可能ならば、リンクまたはコードをしてください提供)

マッズ・ハンセン:

ファイルは、名前が「製品」で始まるの開始タグと終了タグを持つ行が含まれている場合、あなたはできます。

  • 行で始まるたび個片にこの文書を分割するファイルスキャナを使用し<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");
    }

}

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=200733&siteId=1