久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

我可以重新排序現(xiàn)有的 XML 以遵守 XSD

Can I re-order an existing XML to adhere to an XSD(我可以重新排序現(xiàn)有的 XML 以遵守 XSD)
本文介紹了我可以重新排序現(xiàn)有的 XML 以遵守 XSD的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我們使用 Java (org.w3c.dom.Node) 生成 XML,本質(zhì)上使用

We're generating an XML with Java (org.w3c.dom.Node), using essentially

parent.appendChild(doc.createElement(nodeName));

這會(huì)生成一個(gè) XML,其中節(jié)點(diǎn)按調(diào)用appendChild"的順序排序.然而,最終的 XML 需要遵守給定的 XSD.我們的代碼可以確保有效的值類型、必填字段等都可以.然而,我正在為節(jié)點(diǎn)順序而苦苦掙扎.

this generates an XML where nodes are sorted by the order of calling the 'appendChild'. The final XML, however, needs to adhere to a given XSD. Our code can ensure that valid value types, mandatory fields etc. are ok. I am however struggling with the node order.

有沒有辦法:

  • 在插入時(shí)確保節(jié)點(diǎn)順序與 XSD 匹配
  • 創(chuàng)建后根據(jù) XSD 對(duì)整個(gè) XML 重新排序

澄清一下:

我們擁有的是:

<myNodeA>...</myNodeA>
<myNodeC>...</myNodeC>
<myNodeB>...</myNodeB>

XSD 想要的是:

<myNodeA>...</myNodeA>
<myNodeB>...</myNodeB>
<myNodeC>...</myNodeC>

謝謝,西蒙

推薦答案

我之前通過遍歷模式然后從 XML 模型中提取相關(guān)部分并沿途流式傳輸來完成此操作.

I've done this before by traversing the schema and then pulling relevant pieces from the XML model and streaming it along the way.

有多個(gè) xsd 模型庫(kù)可供使用:

There are multiple xsd model libraries to use:

  • xsom
  • 練習(xí)
  • xml架構(gòu)

這是一個(gè)使用 xsom(可以替換為上述之一)和 xom(可以替換為 dom)的示例

Here's an example using xsom (which can be replaced by one of the above) and xom (which can be replaced with dom)

主要:

package main;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;

import node.xom.WrappedDocument;
import nu.xom.Builder;
import nu.xom.Document;
import nu.xom.Element;
import reorder.xsom.UncheckedXMLStreamWriter;
import reorder.xsom.XSVisitorWriteOrdered;

import com.sun.xml.xsom.XSElementDecl;
import com.sun.xml.xsom.XSSchemaSet;
import com.sun.xml.xsom.parser.XSOMParser;

public class ReorderXmlToXsd {
  public static void main(String[] args) throws Exception {
    File unorderedXml = new File("unordered.xml");
    File xsd = new File("your.xsd");
    File orderedXml = new File("ordered.xml");

    XSOMParser p = new XSOMParser();
    p.parse(xsd);
    XSSchemaSet parsed = p.getResult();

    Builder xom = new Builder();
    Document unorderedDoc = xom.build(unorderedXml);
    Element unorderedRoot = unorderedDoc.getRootElement();

    XSElementDecl root = parsed.getElementDecl(
        unorderedRoot.getNamespaceURI(),
        unorderedRoot.getLocalName());

    XMLOutputFactory stax = XMLOutputFactory.newInstance();

    try (OutputStream to = new FileOutputStream(orderedXml)) {
      XMLStreamWriter using = stax.createXMLStreamWriter(to, "UTF-8");

      root.visit(
          new XSVisitorWriteOrdered(
              new WrappedDocument(unorderedDoc),
              new UncheckedXMLStreamWriter(using)));
    }
  }
}

實(shí)際的重新排序邏輯.您可能需要進(jìn)一步修改它.例如,我不必為我的項(xiàng)目處理 xsd:any.

The actual reordering logic. You will probably have to modify this further. For example, I didn't have to deal with the xsd:any for my project.

package reorder.xsom;

import node.WrappedNode;

import com.sun.xml.xsom.*;
import com.sun.xml.xsom.visitor.XSVisitor;

public class XSVisitorWriteOrdered implements XSVisitor {
  private final WrappedNode currNode;
  private final UncheckedXMLStreamWriter writeTo;

  public XSVisitorWriteOrdered(WrappedNode currNode, UncheckedXMLStreamWriter writeTo) {
    this.currNode = currNode;
    this.writeTo = writeTo;
  }

  @Override
  public void attributeUse(XSAttributeUse use) {
    attributeDecl(use.getDecl());
  }

  @Override
  public void modelGroupDecl(XSModelGroupDecl decl) {
    modelGroup(decl.getModelGroup());
  }

  @Override
  public void modelGroup(XSModelGroup model) {
    for (XSParticle term : model.getChildren()) {
      term.visit(this);
    }
  }

  @Override
  public void particle(XSParticle particle) {
    XSTerm term = particle.getTerm();
    term.visit(this);
  }

  @Override
  public void complexType(XSComplexType complex) {
    for (XSAttributeUse use : complex.getAttributeUses()) {
      attributeUse(use);
    }

    XSContentType contentType = complex.getContentType();
    contentType.visit(this);
  }

  @Override
  public void elementDecl(XSElementDecl decl) {
    String namespaceUri = decl.getTargetNamespace();
    String localName = decl.getName();

    for (WrappedNode child : currNode.getChildElements(namespaceUri, localName)) {
      writeTo.writeStartElement(namespaceUri, localName);

      XSType type = decl.getType();
      type.visit(new XSVisitorWriteOrdered(child, writeTo));

      writeTo.writeEndElement();
    }
  }

  @Override
  public void attributeDecl(XSAttributeDecl decl) {
    String namespaceUri = decl.getTargetNamespace();
    String localName = decl.getName();

    WrappedNode attribute = currNode.getAttribute(namespaceUri, localName);
    if (attribute != null) {
      String value = attribute.getValue();
      if (value != null) {
        writeTo.writeAttribute(namespaceUri, localName, value);
      }
    }
  }

  @Override
  public void simpleType(XSSimpleType simpleType) {
    String value = currNode.getValue();
    if (value != null) {
      writeTo.writeCharacters(value);
    }
  }

  @Override
  public void empty(XSContentType empty) {}

  @Override
  public void facet(XSFacet facet) {}

  @Override
  public void annotation(XSAnnotation ann) {}

  @Override
  public void schema(XSSchema schema) {}

  @Override
  public void notation(XSNotation notation) {}

  @Override
  public void identityConstraint(XSIdentityConstraint decl) {}

  @Override
  public void xpath(XSXPath xp) {}

  @Override
  public void wildcard(XSWildcard wc) {}

  @Override
  public void attGroupDecl(XSAttGroupDecl decl) {}
}

稅務(wù)作家:

package reorder.xsom;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

public class UncheckedXMLStreamWriter {
  private final XMLStreamWriter real;

  public UncheckedXMLStreamWriter(XMLStreamWriter delegate) {
    this.real = delegate;
  }

  public void writeStartElement(String namespaceUri, String localName) {
    try {
      real.writeStartElement(namespaceUri, localName);
    } catch (XMLStreamException e) {
      throw new RuntimeException(e);
    }
  }

  public void writeEndElement() {
    try {
      real.writeEndElement();
    } catch (XMLStreamException e) {
      throw new RuntimeException(e);
    }
  }

  public  void writeAttribute(String namespaceUri, String localName, String value) {
    try {
      real.writeAttribute(namespaceUri, localName, value);
    } catch (XMLStreamException e) {
      throw new RuntimeException(e);
    }
  }

  public void writeCharacters(String value) {
    try {
      real.writeCharacters(value);
    } catch (XMLStreamException e) {
      throw new RuntimeException(e);
    }
  }
}

xml 視圖:

package node;

import java.util.List;

import javax.annotation.Nullable;

public interface WrappedNode {
  List<? extends WrappedNode> getChildElements(String namespaceUri, String localName);

  @Nullable
  WrappedNode getAttribute(String namespaceUri, String localName);

  @Nullable
  String getValue();
}

XOM 實(shí)現(xiàn):

文檔:

package node.xom;

import java.util.Collections;
import java.util.List;

import node.WrappedNode;
import nu.xom.Document;
import nu.xom.Element;

public class WrappedDocument implements WrappedNode {
  private final Document d;

  public WrappedDocument(Document d) {
    this.d = d;
  }

  @Override
  public List<WrappedElement> getChildElements(String namespaceUri, String localName) {
    Element root = d.getRootElement();
    if (isAt(root, namespaceUri, localName)) {
      return Collections.singletonList(new WrappedElement(root));
    }
    return Collections.emptyList();
  }

  @Override
  public WrappedAttribute getAttribute(String namespaceUri, String localName) {
    throw new UnsupportedOperationException();
  }

  @Override
  public String getValue() {
    throw new UnsupportedOperationException();
  }

  @Override
  public String toString() {
    return d.toString();
  }

  private static boolean isAt(Element e, String namespaceUri, String localName) {
    return namespaceUri.equals(e.getNamespaceURI())
        && localName.equals(e.getLocalName());
  }
}

元素:

package node.xom;

import java.util.AbstractList;
import java.util.List;

import node.WrappedNode;
import nu.xom.Attribute;
import nu.xom.Element;
import nu.xom.Elements;

class WrappedElement implements WrappedNode {
  private final Element e;

  WrappedElement(Element e) {
    this.e = e;
  }

  @Override
  public List<WrappedElement> getChildElements(String namespaceUri, String localName) {
    return asList(e.getChildElements(localName, namespaceUri));
  }

  @Override
  public WrappedAttribute getAttribute(String namespaceUri, String localName) {
    Attribute attribute = e.getAttribute(localName, namespaceUri);
    return (attribute != null) ? new WrappedAttribute(attribute) : null;
  }

  @Override
  public String getValue() {
    return e.getValue();
  }

  @Override
  public String toString() {
    return e.toString();
  }

  private static List<WrappedElement> asList(final Elements eles) {
    return new AbstractList<WrappedElement>() {
      @Override
      public WrappedElement get(int index) {
        return new WrappedElement(eles.get(index));
      }

      @Override
      public int size() {
        return eles.size();
      }
    };
  }
}

屬性:

package node.xom;

import java.util.List;

import node.WrappedNode;
import nu.xom.Attribute;

class WrappedAttribute implements WrappedNode {
  private final Attribute a;

  WrappedAttribute(Attribute a) {
    this.a = a;
  }

  @Override
  public List<WrappedNode> getChildElements(String namespaceUri, String localName) {
    throw new UnsupportedOperationException();
  }

  @Override
  public WrappedNode getAttribute(String namespaceUri, String localName) {
    throw new UnsupportedOperationException();
  }

  @Override
  public String getValue() {
    return a.getValue();
  }

  @Override
  public String toString() {
    return a.toString();
  }
}

這篇關(guān)于我可以重新排序現(xiàn)有的 XML 以遵守 XSD的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環(huán)繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動(dòng)生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數(shù)據(jù)庫(kù))
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對(duì)象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 夜夜骑av | 麻豆国产精品777777在线 | 涩涩视频在线观看免费 | 国产美女自拍视频 | 精品欧美一区二区中文字幕视频 | 久久久区 | 久久在视频 | 日韩精品1区2区 | 欧洲成人 | 国产999精品久久久 日本视频一区二区三区 | 亚洲一区二区三区欧美 | 日韩在线免费视频 | 午夜影院毛片 | 欧美a免费| 亚洲精品女优 | 在线播放一区二区三区 | 日韩精品免费在线观看 | 亚洲欧美中文字幕在线观看 | 成人三级视频 | 久久久久国产一区二区三区 | 国产在线精品一区二区三区 | 久久久久久久久99精品 | 成人福利网| 成人免费视频网站在线看 | 狠狠操狠狠| 激情免费视频 | 中文一区 | 一区二区三区av | 日韩欧美二区 | 日本久久网站 | h视频在线免费观看 | 91麻豆精品国产91久久久久久 | 中文字幕1区 | 91精品久久久久久久久 | 日韩成人在线播放 | www中文字幕 | 精品久久久久国产 | 日本三级网址 | av播播| 91精品一区 | 国产精品久久久99 |