解析XML注释中的Oracle [复制](Parsing XML Comments in Oracl

2019-07-31 01:53发布

可能重复:
在Oracle PL / SQL XML解析

在下面的XML我需要获得评论“ ADOR Acknowledgement 2

能否请你帮我得到这个

<?xml version="1.0" encoding="utf-8"?> 
<!--ADOR Acknowledgement 2-->  
<AckTransmission xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.irs.gov/efile">
<TransmissionHeader recordCount="1">  
<Jurisdiction>ALABAMA</Jurisdiction>  
<TransmissionId>1946157056</TransmissionId>    
<Timestamp>2012-08-16T01:25:47-05:00</Timestamp> 
<Transmitter>      
  <ETIN>00000</ETIN>     
</Transmitter>  
<ProcessType>T</ProcessType>  
<AgentIdentifier>ACK</AgentIdentifier> 
</TransmissionHeader> 
<Acknowledgement> 
  <SubmissionId>X1684956672</SubmissionId> 
  <EFIN>X16849</EFIN>  
  <GovernmentCode>ALST</GovernmentCode> 
  <SubmissionType>XMLTOM</SubmissionType>
  <TaxYear>9999</TaxYear>  
  <SubmissionCategory>MFET</SubmissionCategory>   
  <AcceptanceStatus>A</AcceptanceStatus>  
  <ContainedAlerts>0</ContainedAlerts>    
  <StatusDate>2012-08-16</StatusDate>   
</Acknowledgement> 
</AckTransmission>

Answer 1:

的组合replaceextract和和XPath表达式可以这样做:

select 
replace(replace(
extract(
xmltype (q'{<?xml version="1.0" encoding="utf-8"?> 
<!--ADOR Acknowledgement 2-->  
<AckTransmission xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.irs.gov/efile">
<TransmissionHeader recordCount="1">  
<Jurisdiction>ALABAMA</Jurisdiction>  
<TransmissionId>1946157056</TransmissionId>    
<Timestamp>2012-08-16T01:25:47-05:00</Timestamp> 
<Transmitter>      
  <ETIN>00000</ETIN>     
</Transmitter>  
<ProcessType>T</ProcessType>  
<AgentIdentifier>ACK</AgentIdentifier> 
</TransmissionHeader> 
<Acknowledgement> 
  <SubmissionId>X1684956672</SubmissionId> 
  <EFIN>X16849</EFIN>  
  <GovernmentCode>ALST</GovernmentCode> 
  <SubmissionType>XMLTOM</SubmissionType>
  <TaxYear>9999</TaxYear>  
  <SubmissionCategory>MFET</SubmissionCategory>   
  <AcceptanceStatus>A</AcceptanceStatus>  
  <ContainedAlerts>0</ContainedAlerts>    
  <StatusDate>2012-08-16</StatusDate>   
</Acknowledgement> 
</AckTransmission>}'),
'/descendant::comment()'
), '<!--'), '-->') as comment_
from dual;


Answer 2:

这里是解析的XML注释代码。

import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;



import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

import org.xml.sax.InputSource;

import org.xml.sax.SAXException;

public class ParseComment {

public static void xmlParser(String xmlString) {
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory
            .newInstance();
    Document document = null;
    try {
        DocumentBuilder documentBuilder = builderFactory
                .newDocumentBuilder();
        InputSource source = new InputSource();
        source.setCharacterStream(new StringReader(xmlString));
        document = documentBuilder.parse(source);
        NodeList nodes = document.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            if (nodes.item(i) instanceof Element) {
                Element element = (Element) nodes.item(i);
                /* add your logic to parse element values here.*/
                System.out.println("Element : " + element.getTextContent());// nodes.item(i).getTextContent());

            }else{
                // This is the comment string.
                String commentString = nodes.item(i).getTextContent();
                System.out.println("Comment: "+commentString);
            }
        }
    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}



}


Answer 3:

在运用了过多的功能XML文本可能性能产生不利影响,所以作为替代,这样的事情应该工作-

SQL> create table my_xml of xmltype;

Table created.

SQL> insert into my_xml values (xmltype('<?xml version="1.0" encoding="utf-8"?> 
                    <!--ADOR Acknowledgement 2-->  
                    <AckTransmission xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.irs.gov/efile">
                    <TransmissionHeader recordCount="1">  
                    <Jurisdiction>ALABAMA</Jurisdiction>  
                    <TransmissionId>1946157056</TransmissionId>    
                    <Timestamp>2012-08-16T01:25:47-05:00</Timestamp> 
                    <Transmitter>      
                      <ETIN>00000</ETIN>     
                    </Transmitter>  
                    <ProcessType>T</ProcessType>  
                    <AgentIdentifier>ACK</AgentIdentifier> 
                    </TransmissionHeader> 
                    <Acknowledgement> 
                      <SubmissionId>X1684956672</SubmissionId> 
                      <EFIN>X16849</EFIN>  
                      <GovernmentCode>ALST</GovernmentCode> 
                      <SubmissionType>XMLTOM</SubmissionType>
                      <TaxYear>9999</TaxYear>  
                      <SubmissionCategory>MFET</SubmissionCategory>   
                      <AcceptanceStatus>A</AcceptanceStatus>  
                      <ContainedAlerts>0</ContainedAlerts>    
                      <StatusDate>2012-08-16</StatusDate>   
                    </Acknowledgement> 
                    </AckTransmission>'));

1 row(s) inserted.      

SQL> select x.comment_text
  from my_xml t
       , xmltable(
         '/descendant::comment()'
          passing t.object_value
          columns comment_text varchar2(200) path '.'
        ) x
   ;

COMMENT_TEXT
----------------------------
ADOR Acknowledgement 2


文章来源: Parsing XML Comments in Oracle [duplicate]