如何使用XML Parser将String转换为XML

时间:2017-10-04 17:26:28

标签: java xml-parsing

<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
    <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
            <xs:element name="BalanceForAwardDetails">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="ProjectID" type="xs:long" minOccurs="0"/>
                        <xs:element name="Appointed_Date" type="xs:dateTime" minOccurs="0"/>
                        <xs:element name="Finalization_Schedule_Date" type="xs:dateTime" minOccurs="0"/>
                        <xs:element name="Final_Proposal_Approval_Date" type="xs:dateTime" minOccurs="0"/>
                        <xs:element name="StartMonth_Date1" type="xs:dateTime" minOccurs="0"/>
                        <xs:element name="StartMonth_Date2" type="xs:dateTime" minOccurs="0"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:choice>
    </xs:complexType>
</xs:element>

将上面的代码存储在名为schemaString的String中。 使用下面的代码将字符串解析为XML

DocumentBuilderFactory factory =DocumentBuilderFactory.newInstance();
     DocumentBuilder builder = factory.newDocumentBuilder();                
     Document document = builder.parse(schemaString);

但是上面的代码给出了如下错误:

java.net.MalformedURLException: no protocol: 
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)

我在做什么事吗?

0 个答案:

没有答案