1

可能重复:
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>
4

3 回答 3

1

和 xpath 表达式的组合可以执行以下replace操作:extract

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;
于 2012-08-20T06:35:02.787 回答
0

在文本上应用太多功能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
于 2012-08-20T19:38:33.330 回答
0

这是从 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();
    }

}



}
于 2012-08-20T06:48:56.747 回答