JavaでのXML解析の詳しい説明
最初の 3 つのメソッドについて簡単に説明します。
DOM メソッド: 個人的な理解は .net の XmlDocument に似ていますが、これは解析時に効率が悪く、メモリを消費し、大規模な XML の解析には適していません。 -ベースの解析、XML の特定の部分が解析されると、特定のイベントがトリガーされる、カスタム解析クラスでイベントがトリガーされたときに何を行うかを定義できます。 .Net システムで可能かどうかはわかりません。同様の方法はありますか?
StAX メソッド: 個人的には、.net に似た XmlReader メソッドが非常に効率的で、メモリ使用量が少なく、大規模な XML の解析に適していると理解しています
ただし、この記事では主に SAX メソッドを紹介します。 JAXB であり、メイン コードのみがここに掲載されています:
import java.util.ArrayList; import java.util.List; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class ConfigParser extends DefaultHandler { private String currentConfigSection; public SysConfigItem sysConfig; public List<InterfaceConfigItem> interfaceConfigList; public List<FtpConfigItem> ftpConfigList; public List<AdapterConfigItem> adapterConfigList; public void startDocument() throws SAXException { sysConfig = new SysConfigItem(); interfaceConfigList = new ArrayList<InterfaceConfigItem>(); ftpConfigList = new ArrayList<FtpConfigItem>(); adapterConfigList = new ArrayList<AdapterConfigItem>(); } public void endDocument() throws SAXException { } public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if (qName.equalsIgnoreCase("Item") && attributes.getLength() > 0) { if (currentConfigSection.equalsIgnoreCase("SysConfigItem")) { sysConfig = new SysConfigItem(attributes); } else if (currentConfigSection.equalsIgnoreCase("InterfaceConfigItems")) { interfaceConfigList.add(new InterfaceConfigItem(attributes)); } else if (currentConfigSection.equalsIgnoreCase("FtpConfigItems")) { ftpConfigList.add(new FtpConfigItem(attributes)); } else if (currentConfigSection.equalsIgnoreCase("AdapterConfigItems")) { adapterConfigList.add(new AdapterConfigItem(attributes)); } } else { currentConfigSection = qName; } } public void endElement(String uri, String localName, String qName) throws SAXException { } public void characters(char ch[], int start, int length) throws SAXException { } }
import java.lang.reflect.Field; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import org.xml.sax.Attributes; public class ConfigItemBase { private static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); public ConfigItemBase() { } /** * 目前只支持几种常用类型 如果需要支持其他类型,请修改这里的代码 * * @param attributes */ public ConfigItemBase(Attributes attributes) { Class<?> cls = this.getClass(); Field[] fields = cls.getDeclaredFields(); for (Field field : fields) { String fieldType = field.getType().getSimpleName(); for (int i = 0; i < attributes.getLength(); i++) { if (attributes.getQName(i).equalsIgnoreCase(field.getName())) { field.setAccessible(true); try { if (fieldType.equalsIgnoreCase("String")) { field.set(this, attributes.getValue(attributes.getQName(i))); } else if (fieldType.equalsIgnoreCase("Integer")) { field.set(this, Integer.valueOf(attributes.getValue(attributes.getQName(i)))); } else if (fieldType.equalsIgnoreCase("Double")) { field.set(this, Double.valueOf(attributes.getValue(attributes.getQName(i)))); } else if (fieldType.equalsIgnoreCase("Date")) { field.set(this, GetDate(attributes.getValue(attributes.getQName(i)))); } else { System.out.println("Warning:Unhandler Field(" + field.getName() + "-" + fieldType + ")"); } } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } break; } } } } public String toString() { String result = ""; Class<?> cls = this.getClass(); String classNameString = cls.getName(); result += classNameString.substring(classNameString.lastIndexOf('.') + 1, classNameString.length()) + ":"; Field[] fields = cls.getDeclaredFields(); for (Field field : fields) { try { result += field.getName() + "=" + field.get(this) + ";"; } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } return result; } /** * 处理时间类型属性(时间格式要求为:yyyy-MM-dd hh:mm:ss) * * @param dateString * @return */ private static Date GetDate(String dateString) { Date date = null; try { date = dateFormat.parse(dateString); } catch (ParseException e) { e.printStackTrace(); } return date; } }
以下は最も便利なものに焦点を当てています: JAXB (Java Architecture for XML Binding)
ここでは例として、より複雑なモバイル BatchSyncOrderRelationReq インターフェイス XML を使用します (誰もがそう思うと思います)これを理解できる人は基本的に十分です)、メッセージの形式は次のとおりです (SvcCont の CDATA コンテンツはメッセージ本文なので、とても不快です):<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <InterBOSS> <Version>0100</Version> <TestFlag>0</TestFlag> <BIPType> <BIPCode>BIP2B518</BIPCode> <ActivityCode>T2101518</ActivityCode> <ActionCode>0</ActionCode> </BIPType> <RoutingInfo> <OrigDomain>BOSS</OrigDomain> <RouteType>routeType</RouteType> <Routing> <HomeDomain>XXXX</HomeDomain> <RouteValue>routeValue</RouteValue> </Routing> </RoutingInfo> <TransInfo> <SessionID>2013041017222313925676</SessionID> <TransIDO>2013041017222313925676</TransIDO> <TransIDOTime>20130410172223</TransIDOTime> <TransIDH></TransIDH> <TransIDHTime></TransIDHTime> </TransInfo> <SNReserve> <TransIDC></TransIDC> <ConvID></ConvID> <CutOffDay></CutOffDay> <OSNTime></OSNTime> <OSNDUNS></OSNDUNS> <HSNDUNS></HSNDUNS> <MsgSender></MsgSender> <MsgReceiver></MsgReceiver> <Priority></Priority> <ServiceLevel></ServiceLevel> <SvcContType></SvcContType> </SNReserve> <Response> <RspType>rspType</RspType> <RspCode>rspCode</RspCode> <RspDesc>rspDesc</RspDesc> </Response> <SvcCont><![CDATA[<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <batchSyncOrderRelationReq> <msgTransactionID>210001BIP2B518130410172223651627</msgTransactionID> <reqNum>2</reqNum> <reqBody> <oprNumb>210001BIP2B518130410172224341871</oprNumb> <subscriptionInfo> <oprTime>oprTime1</oprTime> <actionID>actionId1</actionID> <brand>brand1</brand> <effTime>effTime1</effTime> <expireTime>expireTime1</expireTime> <feeUser_ID>feeUserId1</feeUser_ID> <destUser_ID>destUserId1</destUser_ID> <actionReasonID>actionId1</actionReasonID> <servType>servType1</servType> <subServType>subServType1</subServType> <SPID>spId1</SPID> <SPServID>spServId1</SPServID> <accessMode>accessMode1</accessMode> <servParamInfo> <para_num>0</para_num> <para_info> <para_name></para_name> <para_value></para_value> </para_info> </servParamInfo> <feeType>feeType1</feeType> </subscriptionInfo> </reqBody> <reqBody> <oprNumb>210001BIP2B518130410172224420909</oprNumb> <subscriptionInfo> <oprTime>oprTime2</oprTime> <actionID>actionId2</actionID> <brand>brand2</brand> <effTime>effTime2</effTime> <expireTime>expireTime2</expireTime> <feeUser_ID>feeUserId2</feeUser_ID> <destUser_ID>destUserId2</destUser_ID> <actionReasonID>actionId2</actionReasonID> <servType>servType2</servType> <subServType>subServType2</subServType> <SPID>spId2</SPID> <SPServID>spServId2</SPServID> <accessMode>accessMode2</accessMode> <servParamInfo> <para_num>0</para_num> <para_info> <para_name></para_name> <para_value></para_value> </para_info> </servParamInfo> <feeType>feeType2</feeType> </subscriptionInfo> </reqBody> </batchSyncOrderRelationReq>]]></SvcCont> </InterBOSS>
デコード コードは次のとおりです:
@XmlRootElement(name = "batchSyncOrderRelationReq") @XmlAccessorType(XmlAccessType.FIELD) public class BatchSyncOrderRelationReq extends BossMessage<BatchSyncOrderRelationReq> { @XmlElement(name = "msgTransactionID") private String msgTransactionId = ""; @XmlElement(name = "reqNum") private String reqNum = ""; @XmlElement(name = "reqBody") private List<BatchSyncOrderRelationReqBody> reqBodyList; public BatchSyncOrderRelationReq() { } public String getMsgTransactionId() { return this.msgTransactionId; } public void setMsgTransactionId(String msgTransactionId) { this.msgTransactionId = msgTransactionId; } public String getReqNum() { return this.reqNum; } public void setReqNum(String reqNum) { this.reqNum = reqNum; } public List<BatchSyncOrderRelationReqBody> getReqBodyList() { return this.reqBodyList; } public void setReqBodyList(List<BatchSyncOrderRelationReqBody> reqBodyList) { this.reqBodyList = reqBodyList; } @Override public BatchSyncOrderRelationReq Deserialized(String interBossXmlContent) throws BusinessException { try { // deserialized for head JAXBContext jaxbCxt4Head = JAXBContext.newInstance(MessageHead.class); Unmarshaller unmarshaller4Head = jaxbCxt4Head.createUnmarshaller(); MessageHead head = (MessageHead) unmarshaller4Head.unmarshal(new StringReader(interBossXmlContent)); // deserialized for SyncOrderRelationReq body JAXBContext jaxbCxt4Body = JAXBContext.newInstance(BatchSyncOrderRelationReq.class); Unmarshaller unmarshaller4Body = jaxbCxt4Body.createUnmarshaller(); BatchSyncOrderRelationReq batchSyncOrderRelationReq = (BatchSyncOrderRelationReq) unmarshaller4Body.unmarshal(new StringReader(head.getSvcCont().trim())); batchSyncOrderRelationReq.setHead(head); return batchSyncOrderRelationReq; } catch (JAXBException e) { throw new BusinessException("SyncOrderRelationReq.Deserialized() Error!(" + interBossXmlContent + ")", e); } } }
@XmlAccessorType(XmlAccessType.FIELD) public class BatchSyncOrderRelationReqBody { @XmlElement(name = "oprNumb") private String oprNumb = ""; @XmlElement(name = "subscriptionInfo") private SubscriptionInfo subscriptionInfo; public BatchSyncOrderRelationReqBody(){ } public BatchSyncOrderRelationReqBody(String oprNumb, SubscriptionInfo subscriptionInfo) { this.oprNumb = oprNumb; this.subscriptionInfo = subscriptionInfo; } public String getOprNumb() { return this.oprNumb; } public void setOprNumb(String oprNumb) { this.oprNumb = oprNumb; } public SubscriptionInfo getSubscriptionInfo() { return this.subscriptionInfo; } public void setSubscriptionInfo(SubscriptionInfo subscriptionInfo) { this.subscriptionInfo = subscriptionInfo; } }
@XmlAccessorType(XmlAccessType.FIELD) public class SubscriptionInfo { @XmlElement(name = "oprTime") private String oprTime = ""; @XmlElement(name = "actionID") private String actionId = ""; @XmlElement(name = "brand") private String brand = ""; @XmlElement(name = "effTime") private String effTime = ""; @XmlElement(name = "expireTime") private String expireTime = ""; @XmlElement(name = "feeUser_ID") private String feeUserId = ""; @XmlElement(name = "destUser_ID") private String destUserId = ""; @XmlElement(name = "actionReasonID") private String actionReasonId = ""; @XmlElement(name = "servType") private String servType = ""; @XmlElement(name = "subServType") private String subServType = ""; @XmlElement(name = "SPID") private String spId = ""; @XmlElement(name = "SPServID") private String spServId = ""; @XmlElement(name = "accessMode") private String accessMode = ""; @XmlElement(name = "feeType") private String feeType = ""; public SubscriptionInfo() { } public SubscriptionInfo( String oprTime, String actionId, String brand, String effTime, String expireTime, String feeUserId, String destUserId, String actionReasonId, String servType, String subServType, String spId, String spServId, String accessMode, String feeType) { this.oprTime = oprTime; this.actionId = actionId; this.brand = brand; this.effTime = effTime; this.expireTime = expireTime; this.feeUserId = feeUserId; this.destUserId = destUserId; this.actionReasonId = actionReasonId; this.servType = servType; this.subServType = subServType; this.spId = spId; this.spServId = spServId; this.accessMode = accessMode; this.feeType = feeType; } public String getOprTime() { return this.oprTime; } public void setOprTime(String oprTime) { this.oprTime = oprTime; } public String getActionId() { return this.actionId; } public void setActionId(String actionId) { this.actionId = actionId; } public String getBrand() { return this.brand; } public void setBrand(String brand) { this.brand = brand; } public String getEffTime() { return this.effTime; } public void setEffTime(String effTime) { this.effTime = effTime; } public String getExpireTime() { return this.expireTime; } public void setExpireTime(String expireTime) { this.expireTime = expireTime; } public String getFeeUserId() { return this.feeUserId; } public void setFeeUserId(String feeUserId) { this.feeUserId = feeUserId; } public String getDestUserId() { return this.destUserId; } public void setDestUserId(String destUserId) { this.destUserId = destUserId; } public String getActionReasonId() { return this.actionReasonId; } public void setActionReasonId(String actionReasonId) { this.actionReasonId = actionReasonId; } public String getServType() { return this.servType; } public void setServType(String servType) { this.servType = servType; } public String getSubServType() { return this.subServType; } public void setSubServType(String subServType) { this.subServType = subServType; } public String getSpId() { return this.spId; } public void setSpId(String spId) { this.spId = spId; } public String getSpServId() { return this.spServId; } public void setSpServId(String spServId) { this.spServId = spServId; } public String getAccessMode() { return this.accessMode; } public void setAccessMode(String accessMode) { this.accessMode = accessMode; } public String getFeeType() { return this.feeType; } public void setFeeType(String feeType) { this.feeType = feeType; } }
XML 解析の詳細については、 Java および関連記事は、PHP の中国語 Web サイトに注目してください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック









Mobile XMLからPDFへの速度は、次の要因に依存します。XML構造の複雑さです。モバイルハードウェア構成変換方法(ライブラリ、アルゴリズム)コードの品質最適化方法(効率的なライブラリ、アルゴリズムの最適化、キャッシュデータ、およびマルチスレッドの利用)。全体として、絶対的な答えはなく、特定の状況に従って最適化する必要があります。

単一のアプリケーションで携帯電話でXMLからPDF変換を直接完了することは不可能です。クラウドサービスを使用する必要があります。クラウドサービスは、2つのステップで達成できます。1。XMLをクラウド内のPDFに変換し、2。携帯電話の変換されたPDFファイルにアクセスまたはダウンロードします。

携帯電話でXMLをPDFに直接変換するのは簡単ではありませんが、クラウドサービスの助けを借りて実現できます。軽量モバイルアプリを使用してXMLファイルをアップロードし、生成されたPDFを受信し、クラウドAPIで変換することをお勧めします。クラウドAPIはサーバーレスコンピューティングサービスを使用し、適切なプラットフォームを選択することが重要です。 XMLの解析とPDF生成を処理する際には、複雑さ、エラー処理、セキュリティ、および最適化戦略を考慮する必要があります。プロセス全体では、フロントエンドアプリとバックエンドAPIが連携する必要があり、さまざまなテクノロジーをある程度理解する必要があります。

web.xmlファイルを開くには、次の方法を使用できます。テキストエディター(メモ帳やテキストエディットなど)を使用して、統合開発環境(EclipseやNetBeansなど)を使用してコマンドを編集できます(Windows:Notepad web.xml; Mac/Linux:Open -A Textedit Web.xml)

XMLフォーマットツールは、読みやすさと理解を向上させるために、ルールに従ってコードを入力できます。ツールを選択するときは、カスタマイズ機能、特別な状況の処理、パフォーマンス、使いやすさに注意してください。一般的に使用されるツールタイプには、オンラインツール、IDEプラグイン、コマンドラインツールが含まれます。

XMLをPDFに直接変換するアプリケーションは、2つの根本的に異なる形式であるため、見つかりません。 XMLはデータの保存に使用され、PDFはドキュメントを表示するために使用されます。変換を完了するには、PythonやReportLabなどのプログラミング言語とライブラリを使用して、XMLデータを解析してPDFドキュメントを生成できます。

ほとんどのテキストエディターを使用して、XMLファイルを開きます。より直感的なツリーディスプレイが必要な場合は、酸素XMLエディターやXMLSPYなどのXMLエディターを使用できます。プログラムでXMLデータを処理する場合、プログラミング言語(Pythonなど)やXMLライブラリ(XML.ETREE.ELEMENTTREEなど)を使用して解析する必要があります。

XMLオンラインフォーマットツールは、厄介なXMLコードを自動的に読みやすい形式と維持します。 XMLの構文ツリーを解析し、フォーマットルールを適用することにより、これらのツールはコードの構造を最適化し、その保守性とチームワークの効率を向上させます。
